Capturing repeating group matches in regular expressions

Fredrik Lundh fredrik at pythonware.com
Wed Aug 11 09:45:48 EDT 2004


"gohaku" wrote:

> 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?

finditer compiles the regex for you, of course, and caches the
result.  but if you really want to do it yourself, just do it:

>>> p = re.compile("([a-z]W)")
>>> p.finditer("aWbWcXdXeWfY")
<callable-iterator object at 0x009249B0>
>>> for x in p.finditer("aWbWcXdXeWfY"):
...     print x.groups()
...
('aW',)
('bW',)
('eW',)

</F>






More information about the Python-list mailing list