A regular expression that checks for two specific words

I am looking for a regular expression that checks if line 2 contains certain words. for example, does the string contain a rooster or chicken.

Thanks in advance!

+3
source share
4 answers

An expression to match roosteror henas a complete word (i.e. not when they are part of a longer and different word):

\b(rooster|hen)\b

This is a safety measure to avoid false positives with partial matches.

\bdenotes the word boundary , which is the (zero width) spot between the character in the range of the "character of the word" ( [A-Za-z0-9_]) and any other character. In fact, the above would be:

  • corresponds to "A chicken is either a rooster or a hen."
  • "Chickens are either a roosters or hens." - (rooster|hen)

, , : \b(roosters?|hens?)\b

+6

| . : (rooster|hen)

+2

, / , (rooster|hen). | - alternation.

+2

, ,

(?:word|anotheer word)

http://regexr.com/3gjb2

, (?: diff (

(?:

0
source

Source: https://habr.com/ru/post/1760614/


All Articles