my $beginString = "SEARCH"; my $endString = "TEST"; my $fileContent = do { open(my $fileHandle, $inputFile) or die "Could not open file '$inputFile' $!"; local $/; <$fileHandle>; }; if($fileContent =~ /\b\Q$beginString\E\b(.*?)\b\Q$endString\E\b/s){ my $result = $1; print $result; }