Hello, Monks. I'm trying to understand how greed plays out in a look-ahead assertion, and why it seems to have changed between perl 5.12.4 and 5.16.3.

This works as I expect:

$s = 'alter string-N-U-M-B-E-R-O-N-E and string-N-U-M-B-E-R-T-W-O.'; $s =~ s/(\S+([[:upper:]]-){2,}[[:upper:]]\b)/%$1/g; print "$s\n";
and prints "alter %string-N-U-M-B-E-R-O-N-E and %string-N-U-M-B-E-R-T-W-O." Because the + is greedy, it picks up all nonspace characters before the capital-letters-separated-by-hyphens portion, and because the {2,} is greedy, it picks up the maximum amount of hyphenated capitals.

But these greediness properties do not work when the entire expression is cast as a look-ahead assertion (which allows one to remove the reference in the replacement pattern). Replace the substitution line with:

$s =~ s/(?=\S+([[:upper:]]-){2,}[[:upper:]]\b)/%/g;
to see what I mean. The output is now "alter %s%t%r%i%n%g%-%N%-%U%-%M%-%B%-%E%-%R%-O-N-E and %s%t%r%i%n%g%-%N%-%U%-%M%-%B%-%E%-%R%-T-W-O." This seems to indicate that neither the + nor the {2,} are being greedy anymore.

This worked as I expected in perl 5.12.4, but no longer works in perl 5.16.3. Is there something about the way greed was interpreted in look-ahead assertions that changed between these releases? Thanks for your insight.


In reply to look-ahead greed changed between perl releases? by raygun

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.