#! perl use strict; use warnings; my $string = ' start: end: test code start: real 1 end: real with start: real repeating newlines and more than start: real one instance end: real of the start: real desired string end: real start: end:'; my $header = 'start: real'; my $footer = 'end: real'; $string =~ /.*(^$header$(?:.*)^$footer$)/ms; print "$1\n" if $1;