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