Regular expression syntax for "match nothing"?

I have a python template engine that uses regexp heavily. It uses concatenation, for example:

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

I can change individual substrings (regexp1, regexp2, etc.).

Is there any small and light expression that doesn't match anything that I can use inside the template where I don't need matches? Unfortunately, sometimes โ€œ+โ€ or โ€œ*โ€ is added to the regexp kernel, so I cannot use an empty string - the error โ€œnothing will happen againโ€ will grow.

+48
python regex
Jun 02 '09 at 17:30
source share
7 answers

This should not match anything:

 re.compile('$^') 

Therefore, if you replace regexp1, regexp2 and regexp3 with "$ ^", it is impossible to find a match. If you are not using multi-line mode.




After some tests, I found a better solution.

 re.compile('a^') 

It is impossible to combine and fail before the previous decision. You can replace a with any other character and it will always be impossible to match

+75
Jun 02 '09 at 17:34
source share

(?!) must always match. This is a negative appearance with zero width. If what is in parentheses is the same, then the whole match fails. Given that there is nothing in it, he will tolerate a mismatch for nothing (including nothing).

+23
Jun 02 '09 at 22:02
source share

To match an empty string - even in multi-line mode - you can use \A\Z , therefore:

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

The difference is that \A and \Z are the beginning and end of a line, while ^ and $ they can correspond to the beginning / end of lines, so $^|$^*|$^+ can potentially correspond to a line containing newlines (if the flag is enabled).

And so as not to go anywhere (even an empty line), just try to find the content before the line starts, for example:

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

Since no characters can be before \ A (by definition), this will always not match.

+15
Jun 02 '09 at 17:45
source share
 "()" 

nothing matches and nothing.

+3
Jun 02 '09 at 17:36
source share

Maybe '.{0}' ?

+2
Jun 02 '09 at 17:34
source share

You can use \z..
This is the absolute end of the line, followed by two of them.

If + or * attached at the end, it still refuses to match anything

+1
Jun 02 '09 at 17:58
source share

Or, use some list comprehension to remove useless regex 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 a few comments next to this line of code :-)

0
Jun 02 '09 at 21:56
source share



All Articles