Python Forum

Full Version: Regexp that won't match anything
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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).
$a
??
Google? What is this? :)
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
Quote:Google? What is this? :)

Very interesting results.

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