regexes: How to access multiple matches of a group?
Drop the *
from your regex (so it matches exactly one instance of your pattern). Then use either re.findall(...)
or re.finditer
(see here) to return all matches.
Update:
It sounds like you're essentially building a recursive descent parser. For relatively simple parsing tasks, it is quite common and entirely reasonable to do that by hand. If you're interested in a library solution (in case your parsing task may become more complicated later on, for example), have a look at pyparsing.
The regex
module fixes this, by adding a .captures
method:
>>> m = regex.match(r"(..)+", "a1b2c3")
>>> m.captures(1)
['a1', 'b2', 'c3']