Syntactic Confectionery Delight | |
PerlMonks |
comment on |
( [id://3333]=superdoc: print w/replies, xml ) | Need Help?? |
In other words (I thought) /(b*)/ stops after the first failure, at the start of the string, s/failure/success/ whereas adding the /g would tell the regex engine to keep on trying until it reaches the end of the string. What /g does on a regex depends on context. In The background is that you can write
and get a new match for each iteration:
Update: Answer to the second question That this code produces two replacements for the string of four 'b's remains a puzzle. Why does this appear (this may be my error) that the regex conflates two 'b's rather than all four? A naiive substitution implementation would loop on s/b*/^/, because it would continue to replace the empty string with ^ forever on. Perl is a bit more sophisticated: It detects a zero-width match, and before doing a second substition of a zero-width match at the same position it bumps along, and tries in the next position. So applying s/b*/^/ on abba make these steps:
You can watch it work; I didn't find a way to get the modified string, but at least you can monitor the match positions:
Perl 6 - links to (nearly) everything that is Perl 6.
In reply to Re^2: Greediness of * vs. greediness of +
by moritz
|
|