From: Carlos Date: 2006-12-21T08:02:53+09:00 Subject: Re: Regex question: this should be easy but doesn't work as I expect. [Sam Kong , 2006-12-20 20.10 CET] > What I want is > > "one two three four five" #=> [["one", "three", "five']] > "one two four five" #=> [["one", nil, "five']] > > In short, in the string, "three" might or might not exist. > What regex can match for both? Hi. The problem is that you can very easily NOT match "three" even if it's there. I mean, if you have /1.*3?.*5/ =~ '12345' the engine can succeed matching the 1 at the beginning, the 5 at the end, and trying to match the 3 where the 4 is... and failing, but since it's optional, the overall match succeeds. I think you should try it in two steps: first, try to match with the 3; if that fails, without the "3". Something like: /(?:(1).*(3)|(1)).*(5)/ (The '1' will come either on the first or third array position, you'll have to take care of that.) Maybe there is a simpler solution, but it doesn't come to my mind. Good luck. --