in reply to Matching consecutive "different" regex patterns across multiple lines
Here is a regex solution just for fun:
use strict; use warnings; my $data = do {local $/; <DATA>}; while ($data =~ /^(john.*)\n(?:(?!john).*\n)*?(jacob.*)/mg) { print "$1 > $2\n"; } __DATA__ bhgfsggdsgsg -- john1 weruwearnwrnweuarar jjafdaiuweifweofiuwe jacob - 1.0 -- nfaslf23523525 john2 asfsjldf43tgre john3 asbdfhskafbv3333v sdfahh34ttg sadfhk34t3wtg sdfhk3gfwghhw3 jacob - 2.0
However, I'd also advise line by line processing for this type of problem.
my $buffer; while (<DATA>) { $buffer = $1 if /^(john.*)/; if ($buffer && /^jacob/) { print "$buffer > $_"; $buffer = ''; } }
|
|---|