Hi!
I'm making a text replace and my text to search includes a metacharacter (.
The text to be searched has been stored in a scalar for example $search. The text where the search will be made has been stored for example in a scalar $text. Here is what I'm doing:
$search = "for example (this)"; $text = "looking for example (this)"; $toBeReplaced = "that";
$text =~ s|$search|$toBeReplaced|;
That doesn't work, if you make it this way:
$text =~ s|for example \(this\)|$toBeReplaced|;
works fine, but how can I backslash the metacharacters in a scalar?
You need to "quote" the search string:
s/\Q$search\E/$toBeReplaced/;
Anytime you see a variable used as a search string it should probably be quoted as that's an indicator that the string may come from an source outside of your control and thus might contain metachars.
See Also: perlre(1)
Randy.
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>