You just need to escape the characters with a \. Of course, that's also a special character...
my $match = '(some [very] odd value).'; my $replace = '(modified value)'; # escape anything that might be a potential problem. # note -- we ignore known good values, rather than # encoding known bad values (so if we don't know about, # it gets encoded) $match =~ s/([^\w\s])/\\\1/g; my $regex = qr/$match/; my $SomeStuff = 'This is a test string (value); (some [very] odd value +).'; $SomeStuff =~ s/$regex/$replace/g; print $SomeStuff;
Update: Sorry, I didn't know about \Q ... take a look at reasonablekeith's response. (the logic from mine might be useful if you're trying to force some alternate escape sequence, if there's a possibility of wanting to escape some characters, but allow some to pass through untouched).
In reply to Re: Search and Replace fears
by jhourcle
in thread Search and Replace fears
by poltras
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |