The regex e.+?d
matches an 'e'
and then tries to match as few characters as possible (ungreedy or reluctant), followed by a 'd'
. That is why the following 2 substrings are matched:
extend cup end table
^^^^^^ ^^^
1 2
The regex e.+d
matches an 'e'
and then tries to match as much characters as possible (greedy), followed by a 'd'
. What happens is that the first 'e'
is found, and then the .+
matches as much as it can (till the end of the line, or input):
extend cup end table
^^^^^^^^^^^^^^^^^^^^
The regex engine comes to the end of the line (or input) and can't match the 'd'
in the regex-pattern. So it backtracks to the last 'd'
is saw. That is why the single match is found:
extend cup end table
^^^^^^^^^^^^^^<----- backtrack
1