princepawn has asked for the wisdom of the Perl Monks concerning the following question:

$_ = "1122a44"; while( m/\G(\d\d)/gc ) { print "Found $1\n"; }

output

[tbone@horse1 US]$ perl g.pl Found 11 Found 22 [tbone@horse1 US]$
But I wanted it to print 44 also. But it didn't. I read the perlop manpage to say that the c modifier allowed the matching to continue after a fail.

Is there anyway to match the final 44 if c is not what I want as a modifier?

Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

Replies are listed 'Best First'.
Re: [perlre] \G with /gc does not catch last match for me
by skyknight (Hermit) on Aug 05, 2003 at 17:25 UTC

    The \G meta-character specifies the zero width point after which the last global match left off, in your case, after the 22. So, you're left at 'a44' and trying to match \d\d which of course does not match. If you were instead to go with...

    $_ = "1122a44"; print $1, "\n" while /(\d\d)/g;

    then you would get the effect that you are seeking.

      Oooooooooooooooooohhhhhh.... it an aaaaaaaaaaaaaaaaaanchor...... I get it.... .thanks! :)

      Carter's compass: I know I'm on the right track when by deleting something, I'm adding functionality

        Oooooooooooooooooohhhhhh.... it an aaaaaaaaaaaaaaaaaanchor...... I get it.... .thanks! :)

        You put 'perlre' in the node title. How about reading it? :)

        Juerd # { site => 'juerd.nl', plp_site => 'plp.juerd.nl', do_not_use => 'spamtrap' }