Escaping Perl regexp meta-characters
June 22, 2006 3:38 PM
Subscribe
What's the best way to escape meta-characters in Perl regular expressions?
I'm doing some text parsing in Perl and a lot of my data includes parentheses which Perl interprets as grouping characters in regular experessions, which throws off my results.
For instance if $name = "Homer (Big Tuna) Simpson", then the expression
if($test_name =~ /$name/)
{
...
where $test_name is identical to $name doesn't return the correct results for me because the parentheses in the variable are intepreted as part of the expression and not the string.
I'd like to temporarily escape my strings long enough to test them, but then throw away the escaped version and use the original.
Is there a built-in function or module that would do this, or could someone suggest a quick and dirty sub to handle this?
posted by hwestiii to computers & internet (4 comments total)
2 users marked this as a favorite
$test_name =~ /\Q$name\E/posted by xiojason at 3:46 PM on June 22, 2006