which re a|l|t|e|r|n|a|t|i|v|e matched?

Skip Montanaro skip at pobox.com
Mon Oct 27 12:29:52 EST 2003


I have a long regular expression with the top-level form

    pat = 'A|B|C|D|...'

where there are a couple hundred alternatives, each one being a fairly
simple regular expression (typically just the name of a machine).  Assuming
I've compiled that and match against it:

    matcher = re.compile(pat)
    match = matcher.match(foo)
    if match is not None:
        ...

is there a way to know what alternative was matched?  Note that I'm not
looking for match.group(1).  I want to know which pattern among the various
was matched.  (I realize there might be more than one, but returning just
one is okay.)

If it helps, the regular expression is formed from the keys of a dictionary
like so:

    pat = '('+'|'.join(d.keys())+')'

I'm concatenating them like this so I don't need to make as many re.match()
calls.  I could narrow things down by doing a binary search of the keys(),
but I was hoping for a simple way to do it in one shot.

Thx,

Skip





More information about the Python-list mailing list