On 6/21/07, dblack / wobblini.net <dblack / wobblini.net> wrote:
[snip]
> > So: since * matches "zero or more" characters when it starts the
> > search for .* it matches the absence (the 'zero') and then matches the
> > string (the 'or more').
>
> It's the other way around, though; it matches "hello" *first*, and
> then "".  So the zero-matching (which I admit I'm among those who find
> unexpected) is happening at the end.
>

Ah, but notice:

"hello".scan(/.*$/)
=> ["hello", ""]

"hello".scan(/^.*/)
=> ["hello"]

Strange indeed, but it seems that's how it's working. Although I
suspect I'm not fully grasping the subtleties introduced by the *
character.

Hmm, the more I think on it I think I have an answer:

The /^.*/ pattern specifies that the string must start with anything
(e.g. it must have at least one character) and then zero or more
characters following.

The /.*$/ pattern has no restriction since the anchor is on the side
with the * character. So it's parsed as "zero or more of anything
before the end of the string".

So, if that's correct, you are right that the absence is matched last.
Verified by the fact that the absence follows the string in the
pattern match.

-- Stephen