Groups in regular expressions don't repeat as expected
vlastimil.brom at gmail.com
Thu Apr 21 15:57:22 CEST 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)
> "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
do you mean something like:
>>> import regex
>>> ms = regex.match(r"^([a-z])+$", "abcdef")
['a', 'b', 'c', 'd', 'e', 'f']
Help on built-in function 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.
More information about the Python-list