Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression to match string starting with a specific word

Tags:

regex

People also ask

How do I match a specific word in a regular expression?

Java regex to match specific word Strictly speaking, “\b” matches in these three positions: Before the first character in the data, if the first character is a word character. After the last character in the data, if the last character is a word character.

How do you search for a regex pattern at the beginning of a string?

The meta character “^” matches the beginning of a particular string i.e. it matches the first character of the string. For example, The expression “^\d” matches the string/line starting with a digit. The expression “^[a-z]” matches the string/line starting with a lower case alphabet.

What matches the start of the string?

They are called “anchors”. The caret ^ matches at the beginning of the text, and the dollar $ – at the end. The pattern ^Mary means: “string start and then Mary”.


If you wish to match only lines beginning with stop, use

^stop

If you wish to match lines beginning with the word stop followed by a space:

^stop\s

Or, if you wish to match lines beginning with the word stop, but followed by either a space or any other non-word character you can use (your regex flavor permitting)

^stop\W

On the other hand, what follows matches a word at the beginning of a string on most regex flavors (in these flavors \w matches the opposite of \W)

^\w

If your flavor does not have the \w shortcut, you can use

^[a-zA-Z0-9]+

Be wary that this second idiom will only match letters and numbers, no symbol whatsoever.

Check your regex flavor manual to know what shortcuts are allowed and what exactly do they match (and how do they deal with Unicode).


Try this:

/^stop.*$/

Explanation:

  • / charachters delimit the regular expression (i.e. they are not part of the Regex per se)
  • ^ means match at the beginning of the line
  • . followed by * means match any character (.), any number of times (*)
  • $ means to the end of the line

If you would like to enforce that stop be followed by a whitespace, you could modify the RegEx like so:

/^stop\s+.*$/
  • \s means any whitespace character
  • + following the \s means there has to be at least one whitespace character following after the stop word

Note: Also keep in mind that the RegEx above requires that the stop word be followed by a space! So it wouldn't match a line that only contains: stop


If you want to match anything after a word, stop, and not only at the start of the line, you may use: \bstop.*\b - word followed by a line.

Word till the end of string

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

Only the words starting with stop

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

And if you want to match every string starting with stop, including newlines, use: /^stop.*/s - multiline string starting with stop.


Like @SharadHolani said. This won't match every word beginning with "stop"

. Only if it's at the beginning of a line like "stop going". @Waxo gave the right answer:

This one is slightly better, if you want to match any word beginning with "stop" and containing nothing but letters from A to Z.

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

This would match all

stop (1)

stop random (2)

stopping (3)

want to stop (4)

please stop (5)

But

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

would only match (1) until (3), but not (4) & (5)


/stop([a-zA-Z])+/

Will match any stop word (stop, stopped, stopping, etc)

However, if you just want to match "stop" at the start of a string

/^stop/

will do :D