OT: regular expression matching multiple occurrences of one group
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"? -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: regular expression matching multiple occurrences of one group
pinkisntwell schrieb: 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"? Why is this flagged "OT"? And in python, you can't do that. Groups are based upon the lexical structure of the regexp, and thus have a fixed number of groups. Either use repetitive searches over the input, or postprocess the combined group by e.g. splitting it. Diez -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: regular expression matching multiple occurrences of one group
On Nov 9, 1:53Â pm, pinkisntwell 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/ -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: regular expression matching multiple occurrences of one group
On Mon, 9 Nov 2009 05:53:00 -0800 (PST), pinkisntwell 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"? Is this a ludicrous question, or is it meant for the python group? use strict; use warnings; my @matches = "-c-c-c-c-c" =~ /(-c)/g; print "\...@matches\n"; @matches = (); "-c-a-c-c-c" =~ /(?:(-c)(?{push @matches, $^N})|.)+/x; print "@matches\n"; -sln -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: regular expression matching multiple occurrences of one group
pinkisntwell 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"? Where is the problem? The most straight-forward, simplest approach works just fine: use strict; use warnings; my $s = '-c-c-c-c-c'; my @matched = $s=~/-c/g; print "Found ". @matched . " occurences of '-c':\n"; print join "\n", @matched; Or did you forget to use the g modifier? jue -- http://mail.python.org/mailman/listinfo/python-list
Re: OT: regular expression matching multiple occurrences of one group
En Mon, 09 Nov 2009 12:59:53 -0300, Jon Clements escribió: On Nov 9, 1:53 pm, pinkisntwell 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