Python regular expressions - how to capture multiple groups from a wildcard?

I have a Python regex that contains a group that can be zero or many times, but when I get a list of groups after that, only the last one is present. Example:

re.search("(\w)*", "abcdefg").groups ()

returns a list ('g',)

I need it to return ('a', 'b', 'c', 'd', 'e', ​​'f', 'g',)

Is it possible? How should I do it?

+30
python regex lexical-analysis
Jan 21 '09 at 10:29
source share
2 answers

In addition to the Douglas Leeder solution , here is an explanation:

In regular expressions, the number of groups is fixed. Placing a quantifier in a group does not increase the number of groups (imagine that all indices of other groups increase because the eralier group matches more than once).

Quantifier groups are a way to create a complex subexpression of an atom when you need to match it more than once. The regex engine has no other way than to save the last match only with the group. In short: there is no way to achieve what you want with a single β€œunarmed” regular expression, and you need to find another way.

+25
Jan 21 '09 at 11:19
source share
 re.findall(r"\w","abcdefg") 
+36
Jan 21 '09 at 10:33
source share



All Articles