On 9/14/2016 8:05 AM, Jussi Piitulainen wrote:
Serhiy Storchaka writes:

On 14.09.16 11:28, Steven D'Aprano wrote:

I'm not sure if it makes sense to talk about an "empty regular
expression", or if that is the same as re.compile(''). Presumably if
such a thing makes sense, it would match nothing, from any input at
all.

Actually, it matches anything. re.compile('').match(x) returns
non-false value for any valid x.

It matches the empty string. The .match method returns a match object if
the regex matches at the start of the input. See span:

re.compile('').match('foo')
===> <_sre.SRE_Match object; span=(0, 0), match=''>

There's an empty string at every position, same content, different span:

re.compile('').findall('foo')
===> ['', '', '', '']

To put it slightly differently, regexes match slices. A string of n chars has n+1 empty (len 0) slices - the 2 at the ends and the n-1 between chars.

--
Terry Jan Reedy

--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to