"Zeroing out" the Nth group in a RE

Greg Chapman glc at well.com
Mon Aug 5 16:39:24 CEST 2002


On Mon, 05 Aug 2002 14:04:52 GMT, Greg Chapman <glc at well.com> wrote:

>
>SRE pattern objects have an undocumented scanner method, which returns an object
>capable of repeatedly searching or matching over the given text.  

Of course, as I realized shortly after posting (insert dope slap here), now that
sre patterns have a documented finditer method, there's really no reason to use
the scanner objects for searching.  So here's another rewrite of findall:

import re

def findall(pattern, string):
    pattern = re.compile(pattern)
    iter = pattern.finditer(string)
    if pattern.groups > 0:
        return [m.groups() for m in iter]
    else:
        return [m.group(0) for m in iter]

---
Greg Chapman




More information about the Python-list mailing list