RegEx Problem



Special characters inside of variables cannot be escaped, but Perl tries to use them in a RegEx. Try this example program to see what I mean:

-----PROGRAM-----
#!/usr/bin/perl

$test = "This + test";
$test2 = "+";
if($test =~ /^This $test2 test$/) {
print "WORKS!!!";
}
else {
print "DOESN'T WORK";
}
-----PROGRAM-----

The result will always be "DOESN"T WORK". The same result will occur if you change $test2 = "\+".

To verify that the program isn't at fault, the following always returns "WORKS!!!":

-----PROGRAM-----
#!/usr/bin/perl

$test = "This + test";
if($test =~ /^This \+ test$/) {
print "WORKS!!!";
}
else {
print "DOESN'T WORK";
}
-----PROGRAM-----

How can I tell Perl to ignore "special" characters in a variable when performing a RegExp comparison?

Thanks.
.



Relevant Pages

  • Re: Unmatched [ in regex; marked by <--- HERE .........
    ... here - it's treating my search string as a left bracket that needs to ... The Perl exe is that latest copy from ActiveState. ... If you use special characters inside your reg exp (& expect it not to ...
    (perl.beginners)
  • Re: GREP
    ... digits are allowed ... Regular expressions are an integeral part of Perl. ...
    (comp.lang.c)
  • Re: need help with text wrap problem...
    ... Perl can do read write access to the middle of a file. ... For example, a perl program can gzip files in place, ... see perldoc perlrun, ... Lines in the file are determined by special characters that appear at ...
    (comp.lang.perl.misc)
  • Re: need help with text wrap problem...
    ... Perl, like most procedural computer languages, does not modify files ... A Perl program can read a file in and ... copying the contents to another file, but not copying the parts that ... Lines in the file are determined by special characters that appear at ...
    (comp.lang.perl.misc)
  • Re: matching a pattern with a space or no space??
    ... Perl doesn't have that function. ... How can I regex a data flow that is always ... surrounded by optional white space. ... next name and assignment. ...
    (comp.lang.perl.misc)