Capturing repeating group matches in regular expressions

James Collier james.collier at xtra.co.nz
Wed Aug 11 06:40:54 EDT 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()
('aW','bX','cX','dX','eX','fY')

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.

     Thanks,
        James.



More information about the Python-list mailing list