Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java regular expression - match all strings except few strings

Tags:

java

regex

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?

like image 987
Poni Avatar asked Oct 21 '25 05:10

Poni


1 Answers

Try the following:

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

The | character is an OR operator in regex.

like image 146
Andrew Clark Avatar answered Oct 23 '25 20:10

Andrew Clark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!