0

I have found countless 'match anything except for' examples, but not any for what I want.
I do not want to match "anything" [except for x]!

I want to match "This is a line" but not if the string 'do not' is present. therefore, only line B should contain a match

(A) This is nonsense and none of this should match (B) This is a line and we want part of it to be matched (C) This is a line and we do not want anything to be matched (D) This is nonsense and none of this should match

Can someone please explain how to do this? Are capture groups absolutely necessary? I'm not trying to capture anything but I'll definitely use it if I have to.

I've played around on https://regex101.com/ but I am not getting anywhere. The assistance of regex veterans is greatly appreciated.

user1445967
  • 1,410
  • 2
  • 14
  • 29
  • 1
    Use `\bThis is a line\b(?!.*\bdo not\b)` or `^(?!.*\bdo not\b).*\bThis is a line\b` if `do not` can appear before the `This is a line`. See [here](https://stackoverflow.com/a/39719452/3832970). – Wiktor Stribiżew Nov 27 '18 at 21:05

1 Answers1

1

Use negative look ahead:

a(?!b)

This would match "ax" or "a" but not "ab"

Negative lookahead makes the substring not match if b (example above) is after a (example above)

DownloadPizza
  • 1,734
  • 10
  • 19