I have a Python regular expression that contains a group which can occur zero or many times – but when I retrieve the list of groups afterwards, only the last one is present. Example:
re.search("(w)*", "abcdefg").groups()
this returns the list (‘g’,)
I need it to return (‘a’,’b’,’c’,’d’,’e’,’f’,’g’,)
Is that possible? How can I do it?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
re.findall(r"w","abcdefg")
Method 2
In addition to Douglas Leeder’s solution, here is the explanation:
In regular expressions the group count is fixed. Placing a quantifier behind a group does not increase group count (imagine all other group indexes increment because an eralier group matched more than once).
Groups with quantifiers are the way of making a complex sub-expression atomic, when there is need to match it more than once. The regex engine has no other way than saving the last match only to the group. In short: There is no way to achieve what you want with a single “unarmed” regular expression, and you have to find another way.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0