I'm not a big fan of this tutorial site, but if you read closely what it actually says, it never claims that the regex q(?=u)i
matches the string quit
:
Let's take one more look inside, to make sure you understand the implications of the lookahead. Let's apply q(?=u)i to quit. The lookahead is now positive and is followed by another token. Again, q matches q and u matches u. Again, the match from the lookahead must be discarded, so the engine steps back from i in the string to u. The lookahead was successful, so the engine continues with i. But i cannot match u. So this match attempt fails. All remaining attempts fail as well, because there are no more q's in the string.
I think you might still be confused about how lookaheads work. Either that, or you misread the tutorial site. If the former, then lookaheads work by asserting a match, without actually consuming anything in the string. So the regex q(?=u)i
says to:
match the letter 'q'
lookahead to the next character after 'q' and assert that it is 'u'
then match an 'i' immediately after the 'q'
Of course, the string 'quit' fails, and in fact all strings would fail. The lookahead says to verify that q
is followed by u
, but the following pattern contradicts this by insisting that i
is what follows.