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.


Dave


In reply to Re: RegEx Comparison by davido
in thread RegEx Comparison by perl@1983

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.