Sam Kong wrote:
> Hi all,
>
> -----Code------
>
> re = [
>   /(one).+?(three).+?(five)/,
>   /(one).+?(three)?.+?(five)/,
>   /(one).+?(three|).+?(five)/,
>   /(one).+(three|).+?(five)/
> ]
>
> re.each_with_index do |r, idx|
>   puts idx
>   p "one two three four five".scan(r)
>   p "one two four five".scan(r)
> end
>
> -----Result---------
>
> 0
> [["one", "three", "five"]]
> []
> 1
> [["one", nil, "five"]]
> [["one", nil, "five"]]
> 2
> [["one", "", "five"]]
> [["one", "", "five"]]
> 3
> [["one", "", "five"]]
> [["one", "", "five"]]
>
>
> -----------------
>
> All regexes failed my expectation.
>
> 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?
> 
> Thanks.
> 
> Sam

  /(one) two (?:(three) )?four (five)/