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

Cameron Laird claird at lairds.com
Fri Jan 10 08:33:42 EST 2003


In article <sl51f-6dj.ln1 at news.lairds.org>,
Kyler Laird  <Kyler at news.Lairds.org> wrote:
>	http://www.python.org/doc/current/lib/re-syntax.html
>	(...)
>	    Matches whatever regular expression is inside the
>	    parentheses, and indicates the start and end of a 
>	    group; the contents of a group can be retrieved
>	    after a match has been performed, [...]
>
>Sounds good, so I tried it.
>
>	import re
>
>	text = 'foo foo1 foo2 bar bar1 bar2 bar3'
>
>	test_re = re.compile('([a-z]+)( \\1[0-9]+)+')
>
>	print test_re.findall(text)
>
>I expected the matches to be something like
>	[('foo', [' foo1', ' foo2']), ('bar', [' bar1', ' bar2', ' bar3'])]
>but it's just this.
>	[('foo', ' foo2'), ('bar', ' bar3')]
>
>How do I get to the other groups that were matched?  (Is this
>an FAQ?  I don't know where to start looking.)
			.
			.
			.
Oh, it's matching all the groups.  Does the code below help
explain why?

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

Cameron Laird <Cameron at Lairds.com>
Business:  http://www.Phaseit.net
Personal:  http://phaseit.net/claird/home.html




More information about the Python-list mailing list