This (is)- my first string, is not a regular expression. It's just a string. But when you wrap it in an m// operator, you're telling Perl it's a regular expression. Within regular expressions, parenthesis have meaning. In fact just about every printable non-word character on your keyboard has meaning to the regex engine. So let's look at what you're asking Perl to validate:
use 5.012_002;
use strict;
use warnings;
use YAPE::Regex::Explain;
my $var1 = "This (is)- my first string";
my $var2 = "This (is)- my first string";
say YAPE::Regex::Explain->new($var2)->explain();
And the output:
The regular expression:
(?-imsx:This (is)- my first string)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
This 'This '
----------------------------------------------------------------------
( group and capture to \1:
----------------------------------------------------------------------
is 'is'
----------------------------------------------------------------------
) end of \1
----------------------------------------------------------------------
- my first string '- my first string'
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
So you're asking Perl if "This (is)- my first string" matches "This is- my first string" (the parens are getting interpreted as 'something else' within the regular expression. You have a few options. One is to pass your second string through quotemeta. Another is to simply do a direct comparison using 'eq', rather than a regexp match.
|