jdd has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I would to know why the following regexp:
produces this result:use strict; use warnings FATAL => 'all'; my $string = "aacbbbcac"; my $re1 = qr/((a+)?(b+)?(c))*/; if ($string =~ $re1) { foreach (0..$#-) { printf "Group %d: <%s>\n", $_, substr($string, $-[$_], $+[$_] - $- +[$_]); } }
i.e. $3 is not empty.Group 0: <aacbbbcac> Group 1: <ac> Group 2: <a> Group 3: <bbb> Group 4: <c>
Guessing this is normal, may I ask also if it is possible, with the same regexp, to get a coherent result i.e. $1 equal to $something2.$something3.$something4 ?
ps: some readers may find that I also posted this question to google perl community. I want to keep the original regexp as is. Note that I envisaged to do to perlreapi it this is impossible to have $3 empty with standard perl - if perlreapi permits this manipulation (!?)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Explain a regexp matched group result
by ww (Archbishop) on Oct 28, 2013 at 19:22 UTC | |
by jdd (Acolyte) on Oct 28, 2013 at 19:46 UTC | |
Re: Explain a regexp matched group result
by Laurent_R (Canon) on Oct 28, 2013 at 18:46 UTC | |
by jdd (Acolyte) on Oct 28, 2013 at 18:51 UTC | |
by ig (Vicar) on Oct 28, 2013 at 19:10 UTC | |
by jdd (Acolyte) on Oct 28, 2013 at 19:24 UTC | |
by ig (Vicar) on Oct 28, 2013 at 19:31 UTC | |
|
Back to
Seekers of Perl Wisdom