Nested Lookahead Patterns
The pattern \b[A-Za-z0-9.]+(?=(@(?!(gmail|hotmail|yahoo)))) would match the email address "info@explainth.at" but not the address "info@gmail.com". How does this work?- The main search pattern
\b[A-Za-z0-9.]+simply looks for one or more repetitions of the alphanumeric and . characters starting at a word boundary. - What follows is a nested lookahead pattern
- The
(?=is an instruction to match the main pattern on the condition that the lookahead pattern specified after the = follows immediately afterwards. - The lookahead pattern itself is an instruction to match the @ character - but only if what follows that character is not one of the banned alternatives gmail, hotmail or yahoo.
- The
It is important to appreciate that the lookahead pattern is itself a lookahead. However, in this nested pattern the instruction ?! tells the regex engine match the main pattern, @, if it is not followed by one of the alternatives specified in the lookahead.
Do lookahead patterns always follow the main pattern that we seek to match? Not necessarily. We provide an example next.
Regular Expression Sandbox
Model
Data
Do not wrap the model expression in a /.../ pair. The characters
Do not wrap the model expression in a /.../ pair. The characters
^$.?*!+:=()[]{}|\\ must be escaped - except when then occur inside a character class. Invalid characters will be grayed out.| Result | Left Text | Match | Right Text |
|---|---|---|---|
Download