Expression can be simplified on list

Chris Angelico rosuav at gmail.com
Wed Sep 14 07:40:38 EDT 2016


On Wed, Sep 14, 2016 at 8:46 PM, Serhiy Storchaka <storchaka at gmail.com> wrote:
> 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.

"Matches" has two meanings with a regex. The regular expression
"[A-Za-z]*" matches the three Q's in "123QQQ456", and thus it
successfully matches against that string. In the string "1234", the
regex still successfully matches that string, but it finds nothing,
ergo it matches an empty string in that string. Does a non-empty
string contain an empty string? Well, in the sense that ("" in "spam")
is true, yes, it does - but the empty string didn't match any of those
characters.

ChrisA



More information about the Python-list mailing list