Non-greedy code match algorithm