my $delim = quotemeta '|'; my $s = 'This is my string that has a | in it.'; $s =~ s/($delim)/\\$1/g; print "$s\n"; __END__ This is my string that has a \| in it.