in reply to How to substitute a string containing "\" with same string in quotes.

$string =~ s/\Q$substring\E/\"$substring\"/gi;

\Q..\E uses quotemeta to convert the substring into a regexp pattern that matches the substring.

Replies are listed 'Best First'.
Re^2: How to substitute a string containing "\" with same string in quotes.
by Anonymous Monk on Oct 24, 2008 at 09:53 UTC
    There is no real need to escape the quotes :)
    $string = 'a b c la la a b c la la'; $substring = 'b c'; $string =~ s/\Q$substring\E/"$substring"/gi; print $string; __END__ a "b c" la la a "b c" la la