Python Regex quick question: matching negative character sets

I want to find strings that DO NOT match a specific character sequence. For instance:

sort of

REGEX = r'[^XY]*'

I would like to look for strings that have any number of characters except X and Y next to each other ... REGEX above does not work as it blocks X and Y separately.

+3
source share
2 answers

What about:

if "XY" not in s:
   print "matched"
else
   print "not matched"

Or is it for inclusion in some longer regular expression? Then maybe you want to get a negative expression:

REGEXP="...(?!XY)..."

EDIT: fixed typo

+2
source

There are several ways to do this.

^(?!.*XY).*$

lookahead XY . , , , . .* .

^(?:(?!XY).)*$

(.), , lookahead , XY.

^(?:[^X]+|X(?!Y))*$

, X X, Y.

DOT_ALL, . , - [^X] - .

+1

Source: https://habr.com/ru/post/1788057/


All Articles