perlquestion
raygun
<p>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.</p>
<p>This works as I expect:
<code>
$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";
</code>
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.</p>
<p>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:
<code>
$s =~ s/(?=\S+([[:upper:]]-){2,}[[:upper:]]\b)/%/g;
</code>
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.</p>
<p>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.</p>