Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Answer: How do I escape metacharacters in a user-defined string?

( #21919=categorized answer: print w/ replies, xml ) Need Help??

Q&A > regular expressions > How do I escape metacharacters in a user-defined string? contributed by Perlmage

You could also use quotemeta(), which is how the \Q is implemented. Ie.,

$formula = '(4+5)';
$bit = quotemeta $formula;
if ( $formula =~ /$bit/ ) {
  print "yeah\n"; 
} else { 
  print "Oh no!\n"; 
}
This will handle literal '$' and '@'.

Comment on Answer: How do I escape metacharacters in a user-defined string?
Download Code
Log In?
Username:
Password:

What's my password?
Create A New User
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2015-07-30 07:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls