Capturing repeating group matches in regular expressions

gohaku gohaku at earthlink.net
Wed Aug 11 09:36:17 EDT 2004


On Aug 11, 2004, at 6:40 AM, James Collier wrote:

> To illustrate, what I want is:
>
> >>> re1 = re.compile("([a-z]W)([a-z]X)+([a-z]Y)");
> >>> mo1 = re.match("aWbXcXdXeXfY");
>
problem with re.match, used re1.match
> >>> 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.

This is somewhat off-topic, but I am using finditer:
for m in re.finditer('([a-z]W)','aWbWcXdXeWfY'):
	print m.groups()
#aW
#bW
#eW
I would like to compile the regex instead of using finditer.
How do I do this?

Thank you.
-gohaku




More information about the Python-list mailing list