nzaleski nzaleski - 24 days ago 22
Perl Question

Negative Lookahead RegEx

Why is this not working? Trying to do a negative lookahead. I am trying to pull the numbers from the bins, except in quarantine bin and inspection bin. When I do the code with the ^ in the front is matches all numbers in parenthesis. When I remove the ^ it matches nothing.

Also can you use the "or operator |" inside the negative lookahead? I want to have ^(?!Quarantine_Bin|Inspection_Bin)

I also tried to specifically negate

[^Quarantine_Bin]
and it is still matching.

^(?!Quarantine_Bin)\([0-9]+\)


Data

Quarantine(2),Other_Bin(2),Quarantine_Bin(2),Quarantine_Bin(2),
Quarantine_Bin(5),Inspection_Bin(3),Regular_Bin(5),other(2)

Answer

You should be using a negative lookbehind as:

(?<!(Quarantine|Inspection)_Bin)\([0-9]+\)

RegEx Demo

(?<!(Quarantine|Inspection)_Bin) is a negative lookbehind that asserts failure if there is Quarantine_Bin or Inspection_Bin before our match.

Comments