A regular expression to negate two words - or words

I have a long list, from which the fragment looks something like this:

X1000ABC
X1100ABC
X2000ABC
X2200ABC
X3000ABC
X3300ABC

Can someone explain how I can reconcile all lines except X1000ABCand X2000ABC?

My problem is that I have a long (and potentially growing) list of codes that differ only in 4-digit numbers. Two of these codes (I know of which two) from this list should be excluded.

I tried to use a negative lookahead, but ... I probably received the syntax incorrectly, since I can not get the "or" to work with it. Or ... I just don't understand the look.

Any help appreciated. Thanks in advance.

+3
source share
3 answers

You can use regex:

^(?!X1000ABC$)(?!X2000ABC$)X[0-9]{4}ABC$

Rubular

+4

, JavaScript:

(?!X[12]000ABC)X[0-9]{4}ABC

lookahead. ^ $ ( $ ), .

+2

How about this?

X(?![12]000)\d{4}ABC

tested in python

>>> x
'X1000ABC\nX1100ABC\nX2000ABC\nX2200ABC\nX3000ABC\nX3300ABC'
>>> re.findall("X(?![12]000)\d{4}ABC",x)
['X1100ABC', 'X2200ABC', 'X3000ABC', 'X3300ABC']
+1
source

All Articles