0

Let's say I have the following lines:

1. blah app prod blah blah
2. blah app prod blah ignore
3. blah app staging blah blah
4. blah app staging blah ignore
5. blah app prod blah blah blah ignore blah blah
6. blah blah

I want to write a regex a match all lines that contain "app prod" and do NOT contain "ignore". So I want to grab:

1. blah app prod blah blah

How can I write this regex?

I've tried the following but it's returning lines 1, 2, 5:

.*app prod.*((?!ignore).)*
Johnny Metz
  • 5,977
  • 18
  • 82
  • 146

1 Answers1

2

You should place the negative lookahead which excludes ignore at the start of the pattern:

(?!.*\bignore\b).*\bapp prod\b.*

Demo

The problem with your current pattern .*app prod.*((?!ignore).)* is that it only asserts that ignore does not appear after app prod. But you want this exclusion to apply to ignore appearing anywhere in the line.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    I believe you need a start of string anchor, else for the string, `"7. blah ignore app prod blah"`, your regex would match `"gnore app prod blah"`. – Cary Swoveland Jul 29 '23 at 06:08