You can use quotemeta, either as a function, or as \Q...\E (update: Relashed my slashes, thanks ikegami):
$b = quotemeta $b; $a =~ s/$b/$c/g; #or $a =~ s/\Q$b/$c/g; # \E not needed at end of pattern
In reply to Re: s/$foo/$bar/ when $foo has metacharacters in it
by FunkyMonk
in thread s/$foo/$bar/ when $foo has metacharacters in it
by bcrowell2
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |