Regular expression that checks for 2 specific words

The expresssion to match rooster or hen as a complete word (i.e. not when they are part of a longer, different word):

\b(rooster|hen)\b

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

The \b denotes a word boundary, which is the (zero-width) spot between a character in the range of “word characters” ([A-Za-z0-9_]) and any other character. In effect the above would:

  • match in "A chicken is either a rooster or a hen."
  • not match in "Chickens are either a roosters or hens." – but (rooster|hen) would

As a side note, to allow the plural, this would do: \b(roosters?|hens?)\b

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)