4

I have these strings in an array and I want to iterate this array and find those who match (one line per match check).
The thing is, I can't find the right regex.

Bird
Cat
Dog
Fish
CatDog
DogCat

Currently, with RegexBuddy (damn nice software!!), I have this regex:

(?!Cat\b)\b\w+

I want to add the "Dog" to it so in one regex match try I'll get a match result, you know, like an OR operator.
I want it to check if "Cat" or "Dog" or "Fish" is there in one go.

Any idea?

2
  • Is this your actual problem or a simplified version of it? In the case where you're looking for literal strings and not obscure representations that can take on variable froms you're better off using methods like .contains(), .equals() or the like on a collection Commented Jul 26, 2011 at 16:36
  • @TheCapn - contains() and equals() will probably result in more readable code but I would expect regex to be more efficient for this. Commented Jul 26, 2011 at 16:40

1 Answer 1

7

Try the following:

\b(?!(?:Bird|Cat|Dog|Fish|CatDog|DogCat)\b)\w+

The | character is an OR operator in regex.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.