Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Answer: How do I escape special characters like . / or in a regular expression?

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

Q&A > regular expressions > How do I escape special characters like . / or | in a regular expression? contributed by Codmate

It is possible to use another character (other than "/") to make a regex. A hash symbol can be used for example:
$changeme = "/this/url/would/be/a/nightmare/in/regex/"; $changeme =~ s#would/be#is/not#;
You can also escape all special characters in a regex with "\Q" and "\E" like this:
$changeme = "/this/url/wo*ld/b^e/a/nightmare/in/regex/"; $changeme =~ s/\Qwo*ld/b^e\E/\Qis/not\E/;

Comment on Answer: How do I escape special characters like . / or in a regular expression?
Select or 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 exploiting the Monastery: (6)
As of 2014-11-27 18:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (187 votes), past polls