Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regular expression syntax for "match nothing"?

Tags:

python

regex

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.

like image 867
grigoryvp Avatar asked Jun 02 '09 17:06

grigoryvp


People also ask

How do you match a blank line in regex?

The most portable regex would be ^[ \t\n]*$ to match an empty string (note that you would need to replace \t and \n with tab and newline accordingly) and [^ \n\t] to match a non-whitespace string. Save this answer.

How do you match a regular expression?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" . You also need to use regex \\ to match "\" (back-slash).

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.


2 Answers

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

like image 85
Nadia Alramli Avatar answered Sep 27 '22 16:09

Nadia Alramli


(?!) 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).

like image 40
Chas. Owens Avatar answered Sep 27 '22 17:09

Chas. Owens