Python Forum
Regexp that won't match anything - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Regexp that won't match anything (/thread-2434.html)



Regexp that won't match anything - Ofnuts - Mar-16-2017

Is there a regexp that can't match any string that you throw at it, including the empty string?

(no, this isn't an attempt at nerd sniping, this is for tests).


RE: Regexp that won't match anything - Larz60+ - Mar-16-2017

$a
??


RE: Regexp that won't match anything - Ofnuts - Mar-16-2017

Google? What is this? :)


RE: Regexp that won't match anything - nilamo - Mar-16-2017

Use a negative lookahead to make sure something isn't there... and then check if that thing is actually there?  That way, it's impossible for the check to ever match.
>>> import re
>>> regex = re.compile(r"^(?!spam)spam")
>>> regex.match("")
>>> regex.match("spam")
>>> regex.match("eggs")
>>> regex.match("s")
>>> regex.match("m")
>>> regex.match("pam")
Start-of-line character included so the regex engine doesn't scan the entire line.  If it matters :p


RE: Regexp that won't match anything - Larz60+ - Mar-17-2017

Quote:Google? What is this? :)

Very interesting results.

also search $a by itself
one result: https://www.prairiehome.org/