ObiHill
ObiHill

Reputation: 11876

Regex to find a word then exclude specific words from consideration

I have the following regex which I use to find a word:

(?<=\s)([\w\@\-]+)(?=\s)

I want to further modify this regex to exclude a list of words e.g. do not match if the word is 'cat' or 'dog'.

How do I accomodate the regex to achieve this?

Upvotes: 2

Views: 497

Answers (1)

slier
slier

Reputation: 6740

\b(?!(?:dog|cat)\b)([\w@-]+)\b

Here if you want to match word start/end with @

(?<=\s)(?!(?:dog|cat)(?=\s))([\w\@\-]+)(?=\s)

Upvotes: 7

Related Questions