in reply to Re: Regex troubles...
in thread Regex troubles...

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

Replies are listed 'Best First'.
Re^3: Regex troubles...
by choroba (Cardinal) on Apr 23, 2016 at 17:39 UTC
    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:  <%-{-{-{-<

        Weird, I'm getting a different output:
        a b4 d --- b1 b2 b3 ====== a b7 d --- b5 b6 ====== a b9 d --- b8 ====== a bx d --- ====== --- ======

        This is perl 5, version 20, subversion 1 (v5.20.1) built for x86_64-linux-thread-multi

        ($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,

        This one works for me.

        #!/usr/bin/perl use warnings; use strict; for my $string (qw(ab1b2b3b4d ab5b6b7d ab8b9d abxd ad)) { my @two; my @matches = $string =~ /(a) (?:(b.)(?{ push @two, $2 }))+ (d)/x; @matches and splice @matches, 1, 1, @two; print "@matches\n"; }