Movatterモバイル変換


[0]ホーム

URL:


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

Harald Kirschkirschh 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. ifm.group(i) matches, I would like to know if there is a namecorresponding to i. In the above example this would give me "X" fori==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 itHarald 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-listmailing list

[8]ページ先頭

©2009-2025 Movatter.jp