my $pattern = "Text with\nnewlines in it"; my $text = ......lots of stuff..... $text =~ s/(\Q$pattern\E)/$1$insert/sg;