Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping special characters in Perl regex

I'm trying to match a regular expression in Perl. My code looks like the following:

my $source = "Hello_[version]; Goodbye_[version]";
my $pattern = "Hello_[version]";
if ($source =~ m/$pattern/) {
  print "Match found!"
}

The problem arises in that brackets indicate a character class (or so I read) when Perl tries to match the regex, and the match ends up failing. I know that I can escape the brackets with \[ or \], but that would require another block of code to go through the string and search for the brackets. Is there a way to have the brackets automatically ignored without escaping them individually?

Quick note: I can't just add the backslash, as this is just an example. In my real code, $source and $pattern are both coming from outside the Perl code (either URIEncoded or from a file).

like image 814
CoV Avatar asked Sep 14 '11 21:09

CoV


People also ask

How do you escape a special character in Perl?

Solution. Use a substitution to backslash or double each character to be escaped.

How do I escape a regular expression in Perl?

Because backslash \ has special meaning in strings and regexes, if we would like to tell Perl that we really mean a backs-slash, we will have to "escape" it, by using the "escape character" which happens to be back-slash itself. So we need to write two back-slashes: \\.

What is \b in Perl regex?

Word Boundary: \b The word boundary \b matches positions where one side is a word character (usually a letter, digit or underscore—but see below for variations across engines) and the other side is not a word character (for instance, it may be the beginning of the string or a space character).


3 Answers

\Q will disable metacharacters until \E is found or the end of the pattern.

my $source = "Hello_[version]; Goodbye_[version]";
my $pattern = "Hello_[version]";
if ($source =~ m/\Q$pattern/) {
  print "Match found!"
}

http://www.anaesthetist.com/mnm/perl/Findex.htm

like image 85
martincho Avatar answered Nov 15 '22 08:11

martincho


Use quotemeta():

my $source = "Hello_[version]; Goodbye_[version]";
my $pattern = quotemeta("Hello_[version]");
if ($source =~ m/$pattern/) {
  print "Match found!"
}
like image 37
Pedro Silva Avatar answered Nov 15 '22 07:11

Pedro Silva


You are using the Wrong Tool for the job.

You do not have a pattern! There are NO regex characters in $pattern!

You have a literal string.

index() is for working with literal strings...

my $source = "Hello_[version]; Goodbye_[version]";
my $pattern = "Hello_[version]";
if ( index($source, $pattern) != -1 ) {
    print "Match found!";
}
like image 26
tadmc Avatar answered Nov 15 '22 07:11

tadmc