in reply to Re^2: using lookaround assertions to grab info
in thread using lookaround assertions to grab info

Have you though of extracting the match before the if elsif ... in the foreach loop?
my $section = ''; foreach (@m) { if (/^\s*([\w\s]*?)\s*:/ && $1) { #if we matched and we captured a s +ection label $section = $1; } if ($section eq '...') { ... }

Replies are listed 'Best First'.
Re^4: using lookaround assertions to grab info
by ryantate (Friar) on Jun 03, 2004 at 21:57 UTC
    @m is an array of lines and some lines have multiple sections, ergo this exact code would not work. Or am I missing something?
      It's very likely I could be wrong, I haven't tested it, but I think it would work.
      my $section = ''; foreach (@m) { if ( /^ #match the start of the line \s* #match 0 or more whitespace characters grab as many as we + can ( #start captureing [\w\s]*? #match 0 or more word or whitespace, grab as _few_ + as posible ) #end capture \s* #match 0 or more whitespace characters : #until we reach the a colon /x #x added for the comments && #if the match fails we don't bother checking $1 $1) { #check if we captured any thing if there was only whitesp +ace before the colon we won't have captured any thing $section = $1; #if we actualy found a section label save it } #$section should now be set to the last section label we found. if ($section eq '...') { ... }