I come seeking your insights and wisdom. I "understand" the following example but I can't "comprehend" it (or vise-versa)...
$_='aaab'; print "Matched 1=>$_\n" if /^[a]{2,}(?![b])+/; print "LastMatch1:$&\n"; print "Matched 2=>$_\n" if /^[a]{2,}/; print "LastMatch2:$&\n";
I expected (hoped) that the first RE would fail - but it does not. It seems the negative lookahead causes a "lazy" match on the 1st pattern. That is, $& is set to 'aa' as if it were non-greedy. If I change the {2,} quantifier to {3,} the pattern works as I expect. That is, the pattern match fails. If I change the pattern to {2,3} is behaves the same as {2,}.

The second RE (without the lookahead) behaves greedily as expected setting $& to 'aaa'.

Your insights will be greatly appreciated.

In reply to Greedy match and zero-width negative lookahead assertion by jeffatrobertsdotnet

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.