My glitch. In my mind finditer() returned what findall(), but it returns Match objects.

The implementation based on search(). Seems appropiate.

I just looked in _sre.c, and findall() uses search() and is quite optimized.

It seems that the good implementation would be to write a findalliter() using the current findall() code, and implement findall() and findfirst() by calling that.  

On Thu, Dec 5, 2019 at 10:31 PM Guido van Rossum <guido@python.org> wrote:
On Thu, Dec 5, 2019 at 6:16 PM Juancarlo Añez <apalala@gmail.com> wrote:
It’s unfortunate that these functions aren’t better matched. Why is there a simple-semantics find-everything and a match-semantics find-iteratively and find-one? But I don’t think adding a simple-semantics find-one that works by inefficiently finding all is the right solution.

The proposed implementation for findfirst() is:

return next(finditer(pattern, text, flags=flags), default=default)

Um, finditer() returns a Match object, and IIUC findfirst() should return a string, or a tuple of groups if there's more than one group. So the actual implementation would be a bit more involved. Something like this, to match findall() better:

    for match in re.finditer(pattern, text, flags=flags):
        # Only act on first match
        groups = match.groups()
        if not groups:
            return match.group(0)  # Whole match
        if len(groups) == 1:
            return groups[0]  # One match
        return groups
    # No match, use default
    return default

Alternatively, replace the first line with this:

    match = re.search(pattern, text, flags=flags)
    if match is not None:
 
(There are apparently subtle differences between re.search() and re.findall() -- not sure if they matter in this case.)

And if the point of proposing first is that novices will figure out how to write first(findall(…)) so we don’t need to add findfirst, then I think we need findfirst even more, because novices shouldn’t learn that bad idea.

Yes, my point exactly.
 
I posted another thread to argue in favor of first(), independently of findfirst().

Also agreed, I've observed that as a common pattern.

--
--Guido van Rossum (python.org/~guido)


--
Juancarlo Añez