Capturing repeating group matches in regular expressions

James Collier james.collier at
Wed Aug 11 12:40:54 CEST 2004

Is it possible to capture the results of repeating group matches in the 
python regular expression module?

To illustrate, what I want is:

 >>> re1 = re.compile("([a-z]W)([a-z]X)+([a-z]Y)");
 >>> mo1 = re.match("aWbXcXdXeXfY");

 >>> print mo1.groupsButNotAsWeKnowIt()

instead of

 >>> print mo1.groups()
("aW", "eX", "fY")

... which captures only the last match from the second group.

Of course, one option is to break out the substring containing the 
repeating group and then use split() or findall() within the substring, 
but, but, but ... I'd like to do it in one hit if possible.

I believe someone has raised a similar question before, but I can't find 
a  definitive answer.  It may be so stunningly obvious that nobody ever 
bothers to answer - if so, could some kind soul please humour me and at 
least point me to what I'm not seeing in the Fine Manual.


More information about the Python-list mailing list