SOLUTION: Regexp problem for gurus + possible PEP

Skip Montanaro skip at pobox.com
Mon Jan 7 16:35:05 EST 2002


    Pekka> I managed to find a limited solution ...

    Pekka> Now, If we could get such a version of re.findall() that does not
    Pekka> return empty matches ("").  For example re.findall(pattern, text,
    Pekka> flag), where flag tells, whether empty matches are returned.

    Pekka> Maybe I could use filter or the new finditer -method in sre
    Pekka> -module ? Any ideas are welcomed ?

Maybe. ;-) Looking at your example output, however, I don't see any
completely empty matches.  Sure, some of the second elements are empty
strings, but never are both elements of the tuple empty.  That said, perhaps
this will do the trick (only return pairs where both are not empty):

    matches = [t for t in re.findall(pattern, line)
                 if t[0] != "" and t[1] != ""]

-- 
Skip Montanaro (skip at pobox.com - http://www.mojam.com/)




More information about the Python-list mailing list