which re a|l|t|e|r|n|a|t|i|v|e matched?
Tim Peters
tim.one at comcast.net
Mon Oct 27 13:04:14 EST 2003
[Skip Montanaro]
> 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?
>>> import re
>>> matcher = re.compile('(A)|(B)|(C)').match
>>> matcher('ABC').lastindex
1
>>> matcher('BAC').lastindex
2
>>> matcher('CAB').lastindex
3
>>>
You can't have more than 99 capturing groups in a single regexp, though.
More information about the Python-list
mailing list