9

Is it possible to access the symbolic group name defined in a regular expression with (?P<toto>...) with the equivalent of re.findall()?

Using re.match(), re returns a MatchObject on which the function .group('toto') can be used... I would like to do something close.

Here is an example :

import re
my_str = 'toto=1, bip=xyz, toto=15, bip=abu'
print re.findall('toto=(?P<toto>\d+)\,\sbip=(?P<bip>\w+)', my_str)

It returns :

[('1', 'xyz'), ('15', 'abu')]

I would like to get something like :

[{'toto':'1', 'bip':'xyz'}, {'toto':'15', 'bip':'abu'}]

Is there any simple way to do that? I can't find it anywhere...

Kai
  • 38,985
  • 14
  • 88
  • 103
Thomas Leonard
  • 1,047
  • 11
  • 25

1 Answers1

10

You can't do that with .findall(). However, you can achieve the same effect with .finditer() and some list comprehension magic:

print [m.groupdict() for m in re.finditer('toto=(?P<toto>\d+)\,\sbip=(?P<bip>\w+)', my_str)]

This prints:

[{'toto': '1', 'bip': 'xyz'}, {'toto': '15', 'bip': 'abu'}]

So we loop over each match yielded by .finditer() and take it's .groupdict() result.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343