module re, how to map match group names to their int index
Harald Kirsch
kirschh at lionbioscience.com
Wed Apr 25 05:50:14 EDT 2001
Regular expressions allow to give matching subgroups a name, e.g.:
>>> import re
>>> r = re.compile(" *(?P<X>[a-zA-Z0-9]+) *")
>>> m = r.search(" as9asdfA ")
This allows to extract the parenthesised match by name
>>> m.group('X')
'as9asdfA'
instead of by index
>>> m.group(1)
'as9asdfA'
I would like to have the mapping from the index to the name, i.e. if
m.group(i) matches, I would like to know if there is a name
corresponding to i. In the above example this would give me "X" for
i==1. I can't find anything obvious in the re module.
Do I really have to do things like
if m.group(1)==m.group('X'):
# found it
Harald Kirsch
--
----------------+------------------------------------------------------
Harald Kirsch | kirschh at lionbioscience.com | "How old is the epsilon?"
LION bioscience | +49 6221 4038 172 | -- Paul Erdös
*** Please do not send me copies of your posts. ***
More information about the Python-list
mailing list