Leverage negative lookahead
:
>>> import re
>>> x=r'(?!x)x'
>>> r=re.compile(x)
>>> r.match('')
>>> r.match('x')
>>> r.match('y')
this RE is a contradiction in terms and therefore will never match anything.
NOTE:
In Python, re.match() implicitly adds a beginning-of-string anchor (\A
) to the start of the regular expression. This anchor is important for performance: without it, the entire string will be scanned. Those not using Python will want to add the anchor explicitly:
\A(?!x)x