one more question on regex

Vlastimil Brom vlastimil.brom at gmail.com
Sat Jan 23 05:39:32 EST 2016


2016-01-22 23:47 GMT+01:00 mg <noOne at nowhere.com>:
> Il Fri, 22 Jan 2016 21:10:44 +0100, Vlastimil Brom ha scritto:
>
>> [...]
>
> You explanation of re.findall() results is correct. My point is that the
> documentation states:
>
> re.findall(pattern, string, flags=0)
>     Return all non-overlapping matches of pattern in string, as a list of
> strings
>
> and this is not what re.findall does. IMHO it should be more reasonable
> to get back the whole matches, since this seems to me the most useful
> information for the user. In any case I'll go with finditer, that returns
> in match object all the infos that anyone can look for.
> --
> https://mail.python.org/mailman/listinfo/python-list

Hi,
I don't know the reasoning for this special behaviour of findall, but
it seems to be documented explicitly:
https://docs.python.org/3/library/re.html#re.findall
"... If one or more groups are present in the pattern, return a list
of groups; this will be a list of tuples if the pattern has more than
one group.
finditer is clearly much more robust for general usage.
I only use findall for quick one-line tests (and there one has to
account for this specificities - either by using non capturing groups
or enclosing the whole pattern in a "main" group and use the first
items in the resulting tuples.
vbr



More information about the Python-list mailing list