Stanislav Sedov
10/30/2007 11:36:00 PM
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).
--
Stanislav Sedov
ST4096-RIPE