A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://mail.python.org/pipermail/python-list/2001-April/117511.html below:

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

module re, how to map match group names to their int index module re, how to map match group names to their int indexHarald 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4