Why is the return value of an empty python regexp looking for a match?

When passing an empty string to a regular expression object, the search result is a match object, not None. Should it be None, since there is nothing suitable?

import re

m = re.search("", "some text")
if m is None:
    print "Returned None"
else:
    print "Return a match"

By the way, the use of special characters ^and $gives the same result.

+3
source share
4 answers

An empty template matches any part of the string.

Check this:

import re

re.search("", "ffff")
<_sre.SRE_Match object at 0xb7166410>

re.search("", "ffff").start()
0

re.search("$", "ffff").start()
4

Adding $ does not give the same result. The match is at the end, because this is the only place where it can be.

+11
source

Look at it like this: everything you were looking for was matched, so the search was successful and you got a matching object.

+3

These regular expressions successfully match 0 literal characters.

All lines can be considered as containing an infinite number of empty lines between characters:

'Foo' = '' + '' + ... + 'F' + '' + ... + '' + 'oo' + '' + ...
+1
source

What you need to do is not to check if m is None, but you want to check if m is True:

if m:
    print "Found a match"
else:
    print "No match"

In addition, an empty template matches the entire line.

0
source

Source: https://habr.com/ru/post/1780757/


All Articles