Regular expression to match a line starting with "stop"

How to create a regular expression to match a word at the beginning of a line. We are looking for a stop match at the beginning of the line, and everything can follow it.

For example, the expression should match:

 stop stop random stopping 

Thank.

+51
regex
Aug 6 '09 at 18:05
source share
8 answers

If you want to combine only lines starting with a stop, use

 ^stop 

If you want to match lines starting with stop, then space

 ^stop\s 

Or, if you want to match lines starting with the word stop, and then a space or any other character without a word that you can use (allowing you regular color)

 ^stop\W 

On the other hand, what follows corresponds to the word at the beginning of the line for most regular expression flavors (in these variants, \ w corresponds to the opposite of \ W)

 ^\w 

If your taste has no \ w shortcut, you can use

 ^[a-zA-Z0-9]+ 

Be careful that this second idiom will correspond only to letters and numbers, without a symbol.

Check out the regex flavor guide for what shortcuts are allowed and what exactly they match (and how they relate to Unicode.)

+111
Aug 6 '09 at 18:06
source share

Try the following:

 /^stop.*$/ 

Explanation:

  • / charachters restrict regex (i.e. they are not part of Regex per se)
  • ^ means match at the beginning of a line
  • . and then * means match any character (.), any number of times (*)
  • $ means end of line

If you want this stop to be followed by a space, you can modify RegEx like this:

 /^stop\s+.*$/ 
  • \ s means any space character
  • + after \ s means that there must be at least one white space after the stop word,

Note. Also keep in mind that in RegEx above it is required that there is a space behind the stop word run! Therefore, it will not match a line containing only: stop

+43
Aug 06 '09 at 18:07
source share

If you want to match something after stopping a word, not only at the beginning of a line, you can use: \bstop.*\b - a word followed by a line

Word to the end of the line

Or, if you want to match a word in a string, use \bstop[a-zA-Z]* - only words starting with a stop

Only words starting with a stop

Or the beginning of lines with a stop ^stop[a-zA-Z]* only for a word - only the first word
The whole line ^stop.* Is the first line of the line

And if you want to match every line starting with stop, including new lines, use: /^stop.*/s - a multi-line line starting with stop

+10
Dec 10 '15 at 10:38
source share
 /stop([a-zA-Z])+/ 

Any stop word (stop, stop, stop, etc.) will match.

However, if you just want to match "stop" at the beginning of the line

 /^stop/ 

will do: D

+6
Aug 6 '09 at 18:07
source share

If you want to match everything that starts with "stop", including "stop going", "stop" and "stopping" use:

 ^stop 

If you want to combine the word stop with the words "stop", "stop it", but not "stop" and not "stop", use:

 ^stop\W 
+6
Aug 6 '09 at 18:10
source share

I would suggest using a simple regex approach to this problem. There are too many words that are substrings of other unrelated words, and you are likely to be crazy trying to redefine the simpler solutions already provided.

You want at least a naive drying algorithm (try the Porter barrel, free code in most languages ​​is available there) for text processing in the first place. Store this processed text and pre-processed text in two separate arrays with spatial separation. Make sure that each non-alphabetic character also gets its own index in this array. Whatever the list of words you are filtering, also create them.

The next step is to search for the array indices that match your list of stop words. Remove those from the raw array, and then rejoin the spaces.

This is a bit more complicated, but would be a much more robust approach. If you have doubts about the value of a more NLP-oriented approach, you may need some investigation of clbuttic errors .

0
Aug 6 '09 at 18:22
source share

If you want the word to begin with "stop", you can use the following pattern. "^ Stop. *"

This will be consistent with words starting with a stop followed by something.

0
Dec 04 '17 at 1:37 on
source share

As @SharadHolani said. This will not match every word starting with " stop "

. Only if it at the beginning of the line, like, " stops going ." @Waxo gave the correct answer:

This character is slightly better if you want to combine any word that starts with " stop " and contains only letters from A to Z.

 \bstop[a-zA-Z]*\b 

It will fit all

stop (1)

stop random (2)

stop (3)

want to stop (4)

please stop (5)

But

 /^stop[a-zA-Z]*/ 

will only match (1) to (3), but not (4) and (5)

0
Dec 10 '17 at 20:45
source share



All Articles