The failed output looks like this ........... #data i want ignored, that the program fails to remove ........... ........... SEARCH => #Beginning of data I need .......... .......... .......... #End of data I need, The program successfully removes the data below this line TEST => #### 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; }