It is greedy.

b{0,2} can't magically skip ahead to match characters in a different location in the string; it's constrained to matching at the current position. With that in mind, the match operation matches an a at position 0, then an a at position 1, then zero b at position 2. It wants to match two of them (since it's greedy), but there aren't any to match at position 2! So it settles for matching zero (which you allowed it to do).

It doesn't match one b as you claim.


Note that your usage of (?{ }) won't work in general. Consider the following:

"abdabbc" =~ /a(?:b(?{ say "b"; }))+c/

This prints three b even though the pattern only matches two. To get the correct count, you need

"abdabbc" =~ /a(?:b(?{ local $c = $c + 1; }))+c(?{ say $c; })/

In reply to Re: Must do regex range quantifier { } as a greedy by ikegami
in thread Must do regex range quantifier { } as a greedy by Anonymous Monk

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.