http://www.perlmonks.org?node_id=792803


in reply to how to cut context between first "begin" and "end"?

One approach is to use a regular expression:
#!/usr/bin/perl use common::sense; my $data = join('', <DATA>); $data =~ s{(^|\n)begin\n # begin at start of string or line (.*?\n)*? # lines to skip (non-greedy) end\n} # matching end {$1begin\nend\n}sx; print $data; __DATA__ a begin 1 a 2 end c begin c end a begin 1
Update: added \n anchors and commented the regexp.