From: dblack@... Date: 2007-06-22T01:33:40+09:00 Subject: Re: Do You Understand Regular Expressions? Hi -- On Fri, 22 Jun 2007, Stephen Ball wrote: > On 6/21/07, 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. Yes, that was what I was mostly going by :-) David -- * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com)