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":