in reply to code within a regular expression

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?

Replies are listed 'Best First'.
Re: heh, told ya i'm a pathetic newbie.
by stratkid (Initiate) on May 02, 2002 at 04:50 UTC
    but your explanation was succinct, so now I understand...

    My rational was that I thought the first greedy match for .* (the whole string) would be one match. Then it'd be forced to backup a letter at a time all the way to the the "h" (so 'lori' could match too), and each time would also be another match, thus equaling 7.

    Now if I can ever figure out what the hell dynamic/static scoping is. :-)
    ~stratkid