En Mon, 09 Nov 2009 12:59:53 -0300, Jon Clements <jon...@googlemail.com> escribió:
On Nov 9, 1:53 pm, pinkisntwell <pinkisntw...@gmail.com> wrote:

How can I make a regular expression that will match every occurrence
of a group and return each occurrence as a group match? For example,
for a string "-c-c-c-c-c", how can I make a regex which will return a
group match for each occurrence of "-c"?

As well as what Diez has said, unless you absolutely want regexp's,
and by the sounds of it I'm guessing you may be after something more
flexible: what about the pyparsing module [1]. It handles your
situation quite nicely.

import pyparsing
parser = pyparsing.ZeroOrMore('-c')
parser.parseString('-c-c-c-c-c-c')
(['-c', '-c', '-c', '-c', '-c', '-c'], {})

Not that I like regexes very much, but findall does exactly that:

py> re.findall('-c', '-c-c-c-c-c-c')
['-c', '-c', '-c', '-c', '-c', '-c']

Now, the OP said "return each occurrence as a group match":

py> for g in re.finditer("-c", '-c-c-c-c-c-c'): print g, g.span(), g.group()
...
<_sre.SRE_Match object at 0x00C096E8> (0, 2) -c
<_sre.SRE_Match object at 0x00C09410> (2, 4) -c
<_sre.SRE_Match object at 0x00C096E8> (4, 6) -c
<_sre.SRE_Match object at 0x00C09410> (6, 8) -c
<_sre.SRE_Match object at 0x00C096E8> (8, 10) -c
<_sre.SRE_Match object at 0x00C09410> (10, 12) -c

--
Gabriel Genellina

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to