Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

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

by vroom (His Eminence)
on Jan 21, 2000 at 00:47 UTC ( [id://2260]=perlquestion: print w/replies, xml ) Need Help??

vroom has asked for the wisdom of the Perl Monks concerning the following question:

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

Originally posted as a Categorized Question.

  • Comment on How do I escape special characters like . / or | in a regular expression?

Replies are listed 'Best First'.
Re: How do I escape special characters like . / or in a regular expression?
by vroom (His Eminence) on Jan 21, 2000 at 00:48 UTC
    Precede the special characters with \ ie:
    $filename=~/.*\.txt$/ # matches anything ending with .txt;
Re: How do I escape special characters like . / or in a regular expression?
by arhuman (Vicar) on Jan 23, 2001 at 14:06 UTC
Re: How do I escape special characters like . / or in a regular expression?
by Codmate (Novice) on Aug 29, 2001 at 16:07 UTC
    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/;
Re: How do I escape special characters like . / or in a regular expression?
by ariels (Curate) on Nov 29, 2000 at 17:39 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://2260]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-04-19 16:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found