Groups in regular expressions don't repeat as expected
Vlastimil Brom
vlastimil.brom at gmail.com
Thu Apr 21 09:57:22 EDT 2011
2011/4/20 John Nagle <nagle at animats.com>:
> Here's something that surprised me about Python regular expressions.
>
>>>> krex = re.compile(r"^([a-z])+$")
>>>> s = "abcdef"
>>>> ms = krex.match(s)
>>>> ms.groups()
> ('f',)
>
>...
> "If a group is contained in a part of the pattern that matched multiple
> times, the last match is returned."
>
> That's kind of lame, though. I'd expect that there would be some way
> to retrieve all matches.
>
> John Nagle
> --
> http://mail.python.org/mailman/listinfo/python-list
Hi,
do you mean something like:
>>> import regex
>>> ms = regex.match(r"^([a-z])+$", "abcdef")
>>> ms.captures(1)
['a', 'b', 'c', 'd', 'e', 'f']
>>>
>>> help(ms.captures)
Help on built-in function captures:
captures(...)
captures([group1, ...]) --> list of strings or tuple of list of strings.
Return the captures of one or more subgroups of the match. If there is a
single argument, the result is a list of strings; if there are multiple
arguments, the result is a tuple of lists with one item per argument; if
there are no arguments, the captures of the whole match is returned. Group
0 is the whole match.
>>>
cf.
http://pypi.python.org/pypi/regex
hth,
vbr
More information about the Python-list
mailing list