

(The only exception is Tcl, which treats all groups inside lookahead as non-capturing.) The lookahead itself is not a capturing group. If it contains capturing groups then those groups will capture as normal and backreferences to them will work normally, even outside the lookahead. Any valid regular expression can be used inside the lookahead. You can use any regular expression inside the lookahead (but not lookbehind, as explained below). The positive lookahead construct is a pair of parentheses, with the opening parenthesis followed by a question mark and an equals sign. q (?= u ) matches a q that is followed by a u, without making the u part of the match. Inside the lookahead, we have the trivial regex u. The negative lookahead construct is the pair of parentheses, with the opening parenthesis followed by a question mark and an exclamation point.

Negative lookahead provides the solution: q (?! u ). When explaining character classes, this tutorial explained why you cannot use a negated character class to match a q not followed by a u. Negative lookahead is indispensable if you want to match something not followed by something else. Lookaround allows you to create regular expressions that are impossible to create without them, or that would get very longwinded without them. They do not consume characters in the string, but only assert whether a match is possible or not. That is why they are called “assertions”. The difference is that lookaround actually matches characters, but then gives up the match, returning only the result: match or no match. Lookahead and lookbehind, collectively called “lookaround”, are zero-length assertions just like the start and end of line, and start and end of word anchors explained earlier in this tutorial. Lookahead and Lookbehind Zero-Length Assertions
