Here's a pure-regex approach. It needs Perl version 5.10 or higher due to the use of the \K construct, but that can be worked around if needed.
c:\@Work\Perl\monks>perl -wMstrict -le "use 5.010; ;; my $s = qq{foo\nxyzzy\nline 1\nline 2\nline 3\nline 4\nline 5\nline 6 +\netc.}; print qq{[[$s]]}; ;; my $line = qr{ [^\n]* \n }xms; my $before = qr{ xyzzy \n }xms; my $n = 4; my $match = my ($n_lines) = $s =~ m{ $before \K ((?: $line){$n}) }xm +s; print qq{match: <<$n_lines>>} if $match; " [[foo xyzzy line 1 line 2 line 3 line 4 line 5 line 6 etc.]] match: <<line 1 line 2 line 3 line 4 >>
Update: Actually, just take the \K out and this code, as it stands, works just fine under any Perl version!
Give a man a fish: <%-{-{-{-<
In reply to Re: How to print 5 lines below after a regex match
by AnomalousMonk
in thread How to print 5 lines below after a regex match
by new2perl2016
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |