Python regexp groups: how do I get all groups?

python, regex

Solution

With `re.findall()`

Example:

s = "-ab-cde-fghi-jkl-mn"
re.findall(r'[a-z]+', s)

Output:

['ab', 'cde', 'fghi', 'jkl', 'mn']

Problem

I am puzzled by this: ``` >>> import re >>> re.match(r"(?P<all>-(?P<one>\w+))*","-ab-cde-fghi-jkl-mn").groups() ('-mn', 'mn') >>> re.match(r"(?P<all>-(?P<one>\w+)*)","-ab-cde-fghi-jkl-mn").groups() ('-ab', 'ab') ``` How do I get the list of all terms, ideally like ``` ["ab","cde","fghi","jkl","mn"] ``` but ``` "-ab-cde-fghi-jkl-mn" ``` is fine too. (Please note that I am fully aware of `str.split("-")`. This is a question about `re` - how to match the whole set)

Original source