in reply to Re: substitute instead global matching for nested matches
in thread substitute instead global matching for nested matches

This returns the first element in the 2nd match. I need to get the last element. So the result must be

#4 r2 ! #5 r5 !

And your code is restricted to 3 consecutive elements separated by lines. I need to a more generic solution because my lists are changing variably. Sorry did not mention that clearly in my previous post (the global match ((\S+\s+\S+\s+)*) implies that). So a $fc could be

#4 r1 ! #5 r3 ! r7 ! r10 ! #8 r0 ! r1 !

I need to get the last r member. Any help appreciated.

Replies are listed 'Best First'.
Re^3: substitute instead global matching for nested matches
by GrandFather (Saint) on Mar 18, 2012 at 23:19 UTC

    The following meets your current spec as I understand it:

    use strict; use warnings; my $fc = <<FC; #4 r2 ! #5 r3 ! r7 ! r10 ! #8 r0 ! r1 ! FC print "$1\n$2\n" while $fc =~ /^(#\d+)[^#]*^([^#\n]+)$/gm;

    Prints:

    #4 r2 ! #5 r10 ! #8 r1 !
    True laziness is hard work
      It works! GrandFather, thanks for the tip about pattern matching!