How to get group name of match regular expression in Python?

Chameleon

Question is very basic whatever I do not know how to figure out group name from match. Let me explain in code:

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

How to get group name of a[0].group(0) match - assume that number of named patterns can be larger?

Example is simplified to learn basics.

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

Martijn Pieters

You can get this information from the compiled expression:

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

This uses the RegexObject.groupindex attribute:

A dictionary mapping any symbolic group names defined by (?P<id>) to group numbers. The dictionary is empty if no symbolic groups were used in the pattern.

If you only have access to the match object, you can get to the pattern with the MatchObject.re attribute:

>>> 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}

If all you wanted to know what group matched look at the value; None means a group never was used in a match:

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

The number group never used to match anything because its value is None.

You can then find the names used in the regular expression with:

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

or if there is only ever one group that can match, you can use MatchObject.lastgroup:

name_used = matchobj.lastgroup

As a side note, your regular expression has a fatal flaw; everything that \d matches, is also matched by \w. You'll never see number used where name can match first. Reverse the pattern to avoid this:

>>> 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

but take into account that words starting with digits will still confuse things for your simple case:

>>> 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'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regular expression match group

From Java

Python regular expression file name match

From Dev

Regular expression (.NET): how to get a group in the middle

From Dev

Regular expression match multiple group

From Dev

Named group in regular expression match

From Dev

How to get a name of created table with regular expression?

From Dev

Python Regular Expression not match

From Dev

Regular expression match in python

From Dev

Python regular expression to work for dynamic match group objects

From Dev

How to make regular expression match if a group of symbols is repeated only?

From Dev

How to make regular expression match if a group of symbols is repeated only?

From Dev

Regular Expression Get part of the match

From Dev

How can I get the last match in regular extracor expression in jmeter?

From Dev

How can you get the indices from a regular expression match?

From Dev

Python Regular expression potential match

From Dev

regular expression match issue in Python

From Dev

regular expression match issue in Python

From Dev

Regular expression find group match in quotes

From Dev

retrieving regular expression match group values

From Dev

Regular expression in Vim to match group capture

From Dev

Modifying a group within Regular Expression Match

From Dev

Regular expression only matches full match and not group

From Dev

How to write IF AND regular expression match

From Dev

Regular expression to match xml tag with the same name

From Dev

Regular expression in python to match an logical expression

From Dev

How to match a regular expression with exactly one digit in it using python regex?

From Dev

How to delete all strings that match a regular expression in python?

From Dev

How to perform summation for regular expression match results in python

From Dev

How to Find The Starting Position of the Nth Match of A Regular Expression in Python?

Related Related

  1. 1

    Regular expression match group

  2. 2

    Python regular expression file name match

  3. 3

    Regular expression (.NET): how to get a group in the middle

  4. 4

    Regular expression match multiple group

  5. 5

    Named group in regular expression match

  6. 6

    How to get a name of created table with regular expression?

  7. 7

    Python Regular Expression not match

  8. 8

    Regular expression match in python

  9. 9

    Python regular expression to work for dynamic match group objects

  10. 10

    How to make regular expression match if a group of symbols is repeated only?

  11. 11

    How to make regular expression match if a group of symbols is repeated only?

  12. 12

    Regular Expression Get part of the match

  13. 13

    How can I get the last match in regular extracor expression in jmeter?

  14. 14

    How can you get the indices from a regular expression match?

  15. 15

    Python Regular expression potential match

  16. 16

    regular expression match issue in Python

  17. 17

    regular expression match issue in Python

  18. 18

    Regular expression find group match in quotes

  19. 19

    retrieving regular expression match group values

  20. 20

    Regular expression in Vim to match group capture

  21. 21

    Modifying a group within Regular Expression Match

  22. 22

    Regular expression only matches full match and not group

  23. 23

    How to write IF AND regular expression match

  24. 24

    Regular expression to match xml tag with the same name

  25. 25

    Regular expression in python to match an logical expression

  26. 26

    How to match a regular expression with exactly one digit in it using python regex?

  27. 27

    How to delete all strings that match a regular expression in python?

  28. 28

    How to perform summation for regular expression match results in python

  29. 29

    How to Find The Starting Position of the Nth Match of A Regular Expression in Python?

HotTag

Archive