4

Lets say that this whole thing down there is a string and I would like to match all those words within < and > who end with words dog or bird. It can be done also done by using regex like this:

<.+(dog|bird)>

But I am trying to select by using NOT selector. Only if it NOT ends with cat, it should be selected. Is it actually possible to select something that doesn't contain come word in regex?

<This is dog>
<This is also a dog>
<This is cat>
<Dog and cat>
<Where is dog>
<I am a bird>
<I am another bird>

Thanks,

Vizualni.

jjnguy
  • 136,852
  • 53
  • 295
  • 323
Vizualni
  • 371
  • 3
  • 15

2 Answers2

3

If we're talking about a flavor of regex that supports it, you can use negative lookahead. This:

<(?![^>]*(?:dog|cat)>).+>

will match angle-bracket-enclosed strings that do not end with dog or cat.

chaos
  • 122,029
  • 33
  • 303
  • 309
1

In addition to negative lookahead, you can also use negative lookbehind (match ">" that is not preceded by dog or cat):

<.+(?<!dog|cat)>
Tonttu
  • 1,811
  • 10
  • 8