These are the implementations I added to my library:

```python
def findalliter(pattern, string, flags=0):
    '''
        like finditer(), but with return values like findall()

        implementation taken from cpython/Modules/_sre.c/findall()
    '''
    for m in re.finditer(pattern, string, flags=flags):
        g = m.groups()
        if len(g) == 1:
            yield g[0]
        elif g:
            yield g
        else:
            yield m.group()


def findfirst(pattern, string, flags=0, default=_undefined):
    """
    Avoids using the inefficient findall(...)[0], or first(findall(...))
    """
    return first(findalliter(pattern, string, flags=flags), default=default)
```

Fon *first()*, maybe calling it *take_one()* will clear up
misunderstandings about it's semantics.

-- 
Juancarlo *Añez*
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/2N34FIAA7WBKCI2BQZE2JJAIN5UQZNRC/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to