Regular Expression match

Vlastimil Brom vlastimil.brom at gmail.com
Fri Sep 24 19:53:12 EDT 2010


2010/9/25 AON LAZIO <aonlazio at gmail.com>:
> Hi,
>    Say I have
>    p     = re.compile('a|b')
>    text = 'a'
>    d     = p.findall(text)
>    #d -> ['a']
>    What is the way to find out which pattern p match (the former or latter)?
> I mean without knowing the outcome of p.findall
>    Thanks
>
> --
> Aonlazio
> 'Peace is always the way.' NW
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>

Hi,
I am not sure, I understand the task completely, but maybe named
subpatterns and the groupdict data for the match objects may help?

for m in re.finditer(r"(?P<patternA>A)|(?P<patternB>B)", "QABCZ"):
    print m.group(), m.groupdict()

A {'patternB': None, 'patternA': 'A'}
B {'patternB': 'B', 'patternA': None}

regards,
    vbr



More information about the Python-list mailing list