On Wed, Dec 4, 2019, at 11:05, Guido van Rossum wrote:
> Not so fast. re.search() returns a Match object, while re.finditer()
> and re.findall() return strings. For people who are just interested in
> strings, the Match object is just a distraction. I think I am +1 on
> adding re.findfirst() as proposed by the OP.
Er, findall returns strings, but finditer returns match objects.
Sorry, my bad. Strike finditer() then, the point about findall() being different stands.
And as a side note, PEP 505 would allow the case of wanting the string or None to be written as re.search(...)?.group(0).
Still not a particularly discoverable solution. (Given that regular expressions are often used by less sophisticated users.)
(Since findall returns a list, it can be written as ....findall(...)[0], which is much better than next(iter(....findall(...))).)
Sure, but because both of these fail if there are no matches, we can't use them in general, and findfirst() is meant to address that by having a default rather than failing in that case.
--