OT: regular expression matching multiple occurrences of one group

Jon Clements joncle at googlemail.com
Mon Nov 9 10:59:53 EST 2009


On Nov 9, 1:53 pm, pinkisntwell <pinkisntw... at 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'], {})

hth,
Jon.


[1] http://pyparsing.wikispaces.com/



More information about the Python-list mailing list