From: Stephen Ball Date: 2007-06-21T22:47:23+09:00 Subject: Re: Do You Understand Regular Expressions? On 6/20/07, Daniel DeLorme wrote: > That doesn't really explain why the regexp finds an extra empty string. > I know that zero occurrences is one match but after a greedy match that > matches everything, there should be (logically?) no other match. I am no > stranger to regexps and the result is counter-intuitive to me; I would > consider it a bug. Or at least a very very peculiar behavior. > > Daniel > It's because the pattern /.*/ matches everything, including the absence of everything. Yes, with the proper regexs you can indeed have tea and no tea at the same time. Certainly peculiar, but occasionally useful. 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'). To prevent this you need to indicate to your regular expression that you only want the subset of 'everything' that is actually something. Here are a couple ways to do this: /.+/ will match 1 or more of something, so doesn't return the absence /^.*/ will start the search at the start of the pattern, in a way bypassing the match of zero (the pattern /^.*$/ makes this more clear). /..*/ will match everything after something, this is a modified form of the above that isn't tied to the start of the string -- Stephen