which re a|l|t|e|r|n|a|t|i|v|e matched?
anton muhin
antonmuhin.REMOVE.ME.FOR.REAL.MAIL at rambler.ru
Mon Oct 27 13:34:01 EST 2003
Skip Montanaro wrote:
> 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
>
I'm not sure that you are looking for, but this example might be of
intereset for you:
from __future__ import generators
def enumerate(seq):
n = 0
for e in seq:
yield n, e
n += 1
import re
chars = "abcdefghij"
pat = "|".join(["(?P<m%d>%s)" % (no, c) for no, c in enumerate(chars)])
r = re.compile(pat)
match = r.match("def")
if match:
for name, val in match.groupdict().iteritems():
if val:
print "matched: %s; (no = %d)" % (name, int(name[1:]))
(sorry, Python 2.2)
hth,
anton.
More information about the Python-list
mailing list