in reply to Regex troubles...

That's how repeated captures work. $2 indicates the match that starts at the second group, it can't populate $3 (maybe it should return an array reference?)

I'd solve this in two steps. In the first one, match the whole repetition, than split it into single expressions:

my @source = $text =~ /(regex1)((?:regex2)+)(regex3)/g; my @repeated = $source[1] =~ /.../g; # or split or whatever

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^2: Regex troubles...
by kepler (Scribe) on Apr 20, 2016 at 13:42 UTC
    Hi, Thank you very much for the solution - I've been programming in Perl for some years and I must confess that I wasn't aware of this important detail in regexs that you kindly provided. Kind regards, Kepler
      You can also store the matching parts in an array in a (?{}) expression:
      #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; for my $string (qw( ab1b2b3d ab1b2x )) { my @two; my @matches = $string =~ /(a) (b.(?{ push @two, $2 if defined $2 }))+ (d)/x; say for @matches, '---', @two, '======'; }

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

        That doesn't seem to yield any output that I can understand. Why three 'b1' 'b2' 'b3' from ab1b2b3b4d and nothing from any of the others?

        c:\@Work\Perl\monks>perl -e "use warnings; use strict; use feature qw{ say }; ;; for my $string (qw(ab1b2b3b4d ab5b6b7d ab8b9d abxd ad)) { my @two; my @matches = $string =~ /(a) (b.(?{ push @two, $2 if defined $2 }))+ (d)/x; say for @matches, '---', @two, '======'; } " a b4 d --- b1 b2 b3 ====== a b7 d --- ====== a b9 d --- ====== a bx d --- ====== --- ======

        Update: "This is perl 5, version 14, subversion 4 (v5.14.4) built for MSWin32-x86-multi-thread"


        Give a man a fish:  <%-{-{-{-<