regexes: How to access multiple matches of a group? [duplicate]

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.

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.

Leave a Comment

tech