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:"; if ($string =~/(start: real)((.|\n)*(?!start: )(.|\n)*)*(end: real)/){ print "$&"; }