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

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"; }

Replies are listed 'Best First'.
Re^6: Regex troubles...
by AnomalousMonk (Archbishop) on Apr 23, 2016 at 20:16 UTC

    If I put the for-loop back into the unrolled loop code, I start to see weird stuff again (still Strawberry 5.14.4.1):

    c:\@Work\Perl\monks>perl -wMstrict -le "for my $string (qw(ab1b2b3b4d ab5b6b7d ab8b9d abxd ad)) { my @two; my @matches = $string =~ /(a) (?: (b.) (?{ push @two, $2 }))+ (d)/x; print qq{'$string' -> all:(@matches) -> 2:(@two)}; } " 'ab1b2b3b4d' -> all:(a b4 d) -> 2:(b1 b2 b3 b4) 'ab5b6b7d' -> all:(a b7 d) -> 2:() 'ab8b9d' -> all:(a b9 d) -> 2:() 'abxd' -> all:(a bx d) -> 2:() 'ad' -> all:() -> 2:()
    What Perl version are you using?


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

      Probably related to the changes in perl 5.18.

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

        Indeed. Using a package global instead of a lexical makes everything right as rain:

        c:\@Work\Perl\monks>perl -wMstrict -le "print qq{perl version: $]}; ;; for my $string (qw(ab1b2b3b4d ab5b6b7d ab8b9d abxd ad)) { local our @two; my @matches = $string =~ /(a) (?: (b.) (?{ push @two, $2 }))+ (d)/x; print qq{'$string' -> all:(@matches) -> 2:(@two)}; } " perl version: 5.014004 'ab1b2b3b4d' -> all:(a b4 d) -> 2:(b1 b2 b3 b4) 'ab5b6b7d' -> all:(a b7 d) -> 2:(b5 b6 b7) 'ab8b9d' -> all:(a b9 d) -> 2:(b8 b9) 'abxd' -> all:(a bx d) -> 2:(bx) 'ad' -> all:() -> 2:()
        (I should have realized...)


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

      Try putting it in a file and running from there. Personally I would not trust Window's shell quoting.