vba positive lookahead is too greedy vba positive lookahead is too greedy vba vba

vba positive lookahead is too greedy


Because .+ is greedy by default. The .+ gobbles up every character until it encounters a line break char, or the end-of-input. When that happens, it backtracks to the last MP (the second one in your case).

What you want is to match ungreedy. This can be done by placing a ? after .+:

regexSearch("^.+?(?=[ _-]+MP)", "153 - MP 13.61 to MP 17.65")