-3

I am using Python in case it matters.

I am trying to grab all 6-long numbers from a file: ([0-9]{6}) which works fine.

But I want to ignore any such numbers if they are immediately preceded by "Obsolete #:".

So for example Obsolete #:748275 would get ignored, but not something else 957252.

I am trying stuff like [^Obsolete #:]([0-9]{6}) but it simply doesn't work / grabs the number anyway.

user525966
  • 469
  • 1
  • 6
  • 15
  • Does this answer your question? [Python Regex Negative Lookbehind](https://stackoverflow.com/questions/13947933/python-regex-negative-lookbehind). More info here: https://www.regular-expressions.info/lookaround.html – Pranav Hosangadi Oct 06 '20 at 16:29

1 Answers1

1

Use a negative lookbehind.

(?<!Obsolete #:)\b(\d{6})\b
Try it: https://regex101.com/r/RsMoi9/1

Explanation:

  • (?<!Obsolete #:): Negative lookbehind. Do not match anything that comes after this.
  • \b: Word boundary
  • (\d{6}): Capture six digits
  • \b: Another word boundary
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70