The second one requires a word boundary to exist immediately to the left and to the right of the ==. Your string, "abc == 123" has white-space immediately to the left and right of the ==. Consequently, the word boundaries are between 'c' and 'space', and again between 'space' and '1'. There is no boundary between 'space' and '=', nor between '=' and 'space'.

Thus, the second RE cannot match against your target string. To put it a little more clearly (I hope):

abc[boundary](space)==(space)[boundary]123 <---- What your string loo +ks like. abc(space)[boundary]==[boundary](space)123 <---- What you're trying t +o match.

That second case is impossible; there can never be a word boundary between a space and an equals sign.

Rather than using non-greedy quantifiers, why don't you instead specify what would be considered legal on the left-hand side of the ==. It's likely that your left-hand side needs to be an identifier of some sort. If that's the case, specify what characters are legal:

m/^(\w+)\s*==\s*(\w+)$/

If either side can contain quoted literals that may embed '==', you're going to have to use a real parser instead.


Dave


In reply to Re: Regular Expression Question by davido
in thread Regular Expression Question by VingInMedina

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.