in reply to Matching consecutive "different" regex patterns across multiple lines

buffer it
my @buffer; while ... if( start condition ){ if( @buffer ){ INFO("start condition without end condition, discarding buffer +"); } @buffer = $line; } elsif( end condition ){ print OUTFILE @buffer; undef @buffer; } else { push @buffer, $line; }
  • Comment on Re: Matching consecutive "different" regex patterns across multiple lines
  • Download Code

Replies are listed 'Best First'.
Re^2: Matching consecutive "different" regex patterns across multiple lines
by duelafn (Parson) on Apr 23, 2011 at 13:04 UTC

    More explicitly, Anonymonk is suggesting processing the input line-by-line using something like the following rather than attempting to match a whole chunk at once. Here I am just keeping the john line. If you actually need the lines between, push them to a @buffer as Anonymonk suggests.

    my $john; while (defined(local $_ = <DATA>)) { if (/^(john\d+)$/) { $john = $1; } elsif (/^(jacob \- \d\.0)$/) { if ($john) { print "$john - $1\n"; } else { die "Jacob is not preceded by John!"; } undef $john; } } __DATA__ bhgfsggdsgsg -- john1 weruwearnwrnweuarar jjafdaiuweifweofiuwe jacob - 1.0 -- nfaslf23523525 john2 asfsjldf43tgre john3 asbdfhskafbv3333v sdfahh34ttg sadfhk34t3wtg sdfhk3gfwghhw3 jacob - 2.0

    Good Day,
        Dean