Regular expression syntax for "match nothing"?

PythonRegex

Python Problem Overview


I have a python template engine that heavily uses regexp. It uses concatenation like:

re.compile( regexp1 + "|" + regexp2 + "*|" + regexp3 + "+" )

I can modify the individual substrings (regexp1, regexp2 etc).

Is there any small and light expression that matches nothing, which I can use inside a template where I don't want any matches? Unfortunately, sometimes '+' or '*' is appended to the regexp atom so I can't use an empty string - that will raise a "nothing to repeat" error.

Python Solutions


Solution 1 - Python

This shouldn't match anything:

re.compile('$^')

So if you replace regexp1, regexp2 and regexp3 with '$^' it will be impossible to find a match. Unless you are using the multi line mode.


After some tests I found a better solution

re.compile('a^')

It is impossible to match and will fail earlier than the previous solution. You can replace a with any other character and it will always be impossible to match

Solution 2 - Python

(?!) should always fail to match. It is the zero-width negative look-ahead. If what is in the parentheses matches then the whole match fails. Given that it has nothing in it, it will fail the match for anything (including nothing).

Solution 3 - Python

To match an empty string - even in multiline mode - you can use \A\Z, so:

re.compile('\A\Z|\A\Z*|\A\Z+')

The difference is that \A and \Z are start and end of string, whilst ^ and $ these can match start/end of lines, so $^|$^*|$^+ could potentially match a string containing newlines (if the flag is enabled).

And to fail to match anything (even an empty string), simply attempt to find content before the start of the string, e.g:

re.compile('.\A|.\A*|.\A+')

Since no characters can come before \A (by definition), this will always fail to match.

Solution 4 - Python

Maybe '.{0}'?

Solution 5 - Python

You could use
\z..
This is the absolute end of string, followed by two of anything

If + or * is tacked on the end this still works refusing to match anything

Solution 6 - Python

Or, use some list comprehension to remove the useless regexp entries and join to put them all together. Something like:

re.compile('|'.join([x for x in [regexp1, regexp2, ...] if x != None]))

Be sure to add some comments next to that line of code though :-)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiongrigoryvpView Question on Stackoverflow
Solution 1 - PythonNadia AlramliView Answer on Stackoverflow
Solution 2 - PythonChas. OwensView Answer on Stackoverflow
Solution 3 - PythonPeter BoughtonView Answer on Stackoverflow
Solution 4 - PythonSteefView Answer on Stackoverflow
Solution 5 - PythonShuggyCoUkView Answer on Stackoverflow
Solution 6 - PythonMike MillerView Answer on Stackoverflow