Iterate over group names in a regex match?
djc
slais-www at ucl.ac.uk
Tue Jan 19 12:23:32 EST 2010
Brian D wrote:
> Here's a simple named group matching pattern:
>
>>>> s = "1,2,3"
>>>> p = re.compile(r"(?P<one>\d),(?P<two>\d),(?P<three>\d)")
>>>> m = re.match(p, s)
>>>> m
> <_sre.SRE_Match object at 0x011BE610>
>>>> print m.groups()
> ('1', '2', '3')
>
> Is it possible to call the group names, so that I can iterate over
> them?
>
> The result I'm looking for would be:
>
> ('one', 'two', 'three')
print(m.groupdict())
{'one': '1', 'three': '3', 'two': '2'}
>>> print(m.groupdict().keys())
['one', 'three', 'two']
--
David Clark, MSc, PhD. Dept of Information Studies
Systems & Web Development Manager University College London
UCL Centre for Publishing Gower Str London WCIE 6BT
More information about the Python-list
mailing list