How is the following behavior explained? (running with Ruby 2.4.2)
> "hello\r\n".sub(/e*/, "")
=> "hello\r\n"
> "hello\r\n".sub(/h*/, "")
=> "ello\r\n"
> "hello\r\n".sub(/e+/, "")
=> "hllo\r\n"
> "hello\r\n".sub(/(\r|\n)*/, "")
=> "hello\r\n"
> "hello\r\n".sub(/(\r|\n)+/, "")
=> "hello"
For (1), how the e
is not matched and replaced by ""
, versus (2) the h
is? And then when it is e+
, then it is matched? (so e*
is "non-greedy"? Isn't it by default greedy?)
It is similar for the 4th and 5th cases. I know I can use gsub
, but how is the behavior of sub
explained?