module re, how to map match group names to their int index

Andrew Kuchling akuchlin at mems-exchange.org
Wed Apr 25 10:26:54 EDT 2001


Harald Kirsch <kirschh at lionbioscience.com> writes:
> 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.

You'd have to invert the .groupindex dictionary attribute of the compiled
pattern.  Following your example:

>>> r.groupindex
{'X': 1}

--amk




More information about the Python-list mailing list