in reply to Search and Replace fears

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).

Replies are listed 'Best First'.
Re^2: Search and Replace fears
by reasonablekeith (Deacon) on Jul 11, 2005 at 15:17 UTC
    You don't need to do all this, it's what \Q is for!

    Perhaps I should have been more explicit...

    my $string = 'abcdef'; my $match = '[a-z]'; $string =~ s/\Q$match/NOT REPLACED/; print "$string\n"; $string =~ s/$match/REPLACED/; print "$string\n"; __OUTPUT__ abcdef REPLACEDbcdef
    have a look in ...
    perldoc perlre
    ---
    my name's not Keith, and I'm not reasonable.