This is a common misunderstanding. Lazy quantifiers do not guarantee the shortest possible match. They only make sure that the current quantifier, from the current position, does not match more characters than needed for an overall match.
If you truly want to ensure the shortest possible match, you need to make that explicit. In this case, this means that instead of .*?
, you want a subregex that matches anything that is neither aaa
nor bbb
. The resulting regex will therefore be
aaa(?:(?!aaa|bbb).)*bbb