How do I get to *all* of the groups of an re search?

P at draigBrady.com P at draigBrady.com
Fri Jan 10 10:29:48 EST 2003


Cameron Laird wrote:
> I'm clumsy with REs--I don't immediately see how to achieve
> your desired result.  I can quickly observe that
>   import re
> 
>   text = 'foo foo1 foo2 bar bar1 bar2 bar3'
> 
>   test_re = re.compile('([a-z]+)(( \\1[0-9]+)+)')
> 
>   print test_re.findall(text) 
> yields
>   [('foo', ' foo1 foo2', ' foo2'), ('bar', ' bar1 bar2 bar3', ' bar3')]
> One of us will probably get an RE that properly listifies these
> within the next day ...

No need to do everything in the RE.

1. test_re = re.compile('([a-z]+)((?: \\1[0-9]+)+)')
2. just .split() the resultant ' foo1 foo2' etc.

Pádraig.





More information about the Python-list mailing list