From: Daniel Waite Date: 2007-10-31T08:51:51+09:00 Subject: Re: Why, oh, why, little regexp? Stanislav Sedov wrote: > On Wed, Oct 31, 2007 at 08:14:01AM +0900 Daniel Waite mentioned: >> 'cost * tax'.match(/([a-z]+)*/).to_a >> => ["cost", "cost"] >> >> Why? >> > > Well, the regexp always matches the longest possible string. > What did you wrote is effectively equialent to ([a-z]*). > The single regexp can't match multiple strings, it always matches > one. It can't match the space after the 'cost' either, since this > symbol wasn't included to your regexp. > > In case, if you want to match two words, you should write e.g. > ([[:alpha:]]+)[[:space:]]+([[:alpha:]]+) > This regexp will match two words separated by a space. > Regexp can't match an undefined number of words, you should know > in advance which number of words you want to match. > > For more infor on regexps see e.g. re_format(7). Hmm... if what you say is true, why does the second poster's solution capture multiple words? Wait, I know why. String#scan is different than string#match. Interesting... So how does that work if I wanted to match ALL occurrences of \w+ WITHOUT scan? -- Posted via http://www.ruby-forum.com/.