in python code
regular_expression = ""
p = re.compile(regular_expression)
result = p.match("some strings")
if result:
print("Match")
else:
print("No Match")
I want to make regular_expression which always can’t find match in any situation.
I though “”(no words) would work. but sadly it always return “Match”.
I also want to know why it works like that. how “” and “.*” can be same?
One simple regex is
meaning “Assert that it’s impossible to match the empty string”.
Your “empty regex” always matches precisely because it’s always possible to match the empty string.
.match()does not require the regex to match the entire string, it only requires that it should match at the start of the string, which it does.If you want the regex to match the entire string, then use anchors:
only matches an empty string.
EDIT:
In most regex flavors,
\zis the true end-of-string anchor whereas\Zcan also match before a final newline character at the end of the string. In Python however,\Zbehaves as a true end-of-string anchor.