Reputation: 11686
Does there exist a regular expression that matches no strings? If so, what is it?
To be precise, I'm looking for a regular expression r
such that the following Python code outputs True
for any string s
:
import re
print(re.match(r, s) is None)
Upvotes: 1
Views: 327
Reputation: 1207
The following regex should match no strings. It will match any single character which is neither a whitespace character, nor a nonwhitespace character.
[^\S\s]
Upvotes: 1
Reputation: 30580
If your regex engine supports lookahead (which Python's does):
(?!)
Otherwise something like this would work too:
^\b$
A word break can't occur by itself!
Or,
$a^
The end of the string can't match at the start of the string unless the string is empty, and we prevent it from being empty by requiring that we match at least one character.
Then again, ^
/$
/\b
are really just lookarounds in disguise.
Upvotes: 4