How to get match regular expression group name in Python?

The question is very simple, regardless of the fact that I do not know how to determine the name of a group from a match. Let me explain the code:

import re    
a = list(re.finditer('(?P<name>[^\W\d_]+)|(?P<number>\d+)', 'Ala ma kota'))

How to get the name of the a[0].group(0)match group - suppose that the number of named templates can be more?

The example is simplified to learn the basics.

I can invert the match a[0].groupdict(), but it will be slow.

+4
source share
2 answers

You can get this information from a compiled expression:

>>> pattern = re.compile(r'(?P<name>\w+)|(?P<number>\d+)')
>>> pattern.groupindex
{'name': 1, 'number': 2}

The attribute is used here RegexObject.groupindex:

, , (?P<id>), . , .

, MatchObject.re:

>>> a = list(re.finditer(r'(?P<name>\w+)|(?P<number>\d+)', 'Ala ma kota'))
>>> a[0]
<_sre.SRE_Match object at 0x100264ad0>
>>> a[0].re.groupindex
{'name': 1, 'number': 2}

, , ; None , :

>>> a[0].groupdict()
{'name': 'Ala', 'number': None}

number , None.

, , :

names_used = [name for name, value in matchobj.groupdict().iteritems() if value is not None]

, , MatchObject.lastgroup:

name_used = matchobj.lastgroup

; , \d, \w. number, name . , :

>>> for match in re.finditer(r'(?P<name>\w+)|(?P<number>\d+)', 'word 42'):
...     print match.lastgroup
... 
name
name
>>> for match in re.finditer(r'(?P<number>\d+)|(?P<name>\w+)', 'word 42'):
...     print match.lastgroup
... 
name
number

, , , :

>>> for match in re.finditer(r'(?P<number>\d+)|(?P<name>\w+)', 'word42 42word'):
...     print match.lastgroup, repr(match.group(0))
... 
name 'word42'
number '42'
name 'word'
+17

, : r'(?P<name>\w+)|(?P<number>\d+)'. , reg expr , \w - , , , \d. r'(?P<number>\d+)|(?P<name>\w+)', \d \w. , lastgroup , ..:   [m.lastgroup for m in re.finditer(r'(?P<number>\d+)|(?P<name>\w+)', 'Ala ma 123 kota')] :   ['name', 'name', 'number', 'name']

+3

All Articles