I’m looking for a regular expression that checks whether a string contains 2 specific words.
e.g. Whether the string contains rooster or hen.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The expresssion to match
roosterorhenas a complete word (i.e. not when they are part of a longer, different word):This is a safety measure to avoid false positives with partial matches.
The
\bdenotes 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:"A chicken is either a rooster or a hen.""Chickens are either a roosters or hens."– but(rooster|hen)wouldAs a side note, to allow the plural, this would do:
\b(roosters?|hens?)\b