Why would you expect $i to be 7? You didn't say. I would expect $i to be 10. The reason is that .* will match every character it comes across. The * is a "greedy" quantifier. This means that .* matches all the way to the end of the string, incrementing $i ten times. After that the engine starts backtracking trying to complete the match, eventually taking 6 characters away from .*, but never executing the .* section again.
Of course, this is a vast simplification. If you're interested in truely understanding regular expressions, I suggest you pick up a copy of "Mastering Regular Expressions" from O'Reilly Press. The Camel Book, for all its value, is not nearly enough to allow you to fully grok the regular expression in all its glory.
-sam
Side note: I wonder if the code above will always set $i to 10. Couldn't some future regex optimization intuit the limit on the .* section and end up setting $i to 4?
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.