The regular expression e.+?d matches 'e' , and then tries to match as few characters as possible (jagged or reluctant), followed by 'd' . That's why the following 2 substrings are matched:
extend cup end table ^^^^^^ ^^^ 1 2
The regular expression e.+d matches 'e' , and then tries to match as many characters (greedy) as possible, and then 'd' . It happens that the first 'e' found, and then .+ Matches as much as it can (to the end of the line or input):
extend cup end table ^^^^^^^^^^^^^^^^^^^^
The regex engine comes to the end of a line (or input) and cannot match the 'd' in the regex pattern. Thus, he returns to the last 'd' . This is why one match was found:
extend cup end table ^^^^^^^^^^^^^^<
source share