OT: regular expression matching multiple occurrences of one group

P

pinkisntwell

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"?
 
D

Diez B. Roggisch

pinkisntwell said:
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
 
J

Jon Clements

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.
(['-c', '-c', '-c', '-c', '-c', '-c'], {})

hth,
Jon.


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

sln

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 "\n@matches\n";

@matches = ();
"-c-a-c-c-c" =~ /(?:(-c)(?{push @matches, $^N})|.)+/x;
print "@matches\n";

-sln
 
J

Jürgen Exner

pinkisntwell said:
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
 
G

Gabriel Genellina

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.
(['-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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top