Lookahead Patterns
Consider the sentence "For more info contact us at info@explainth.at" and imagine that you want to write an automated email which begins thus:
Dear Info,
Thank you very much for... If you use the string manipulation capabilities of a programming language such as Javascript, Delphi etc you would probably follow the steps below
- Locate the position of the @ character.
- Locate the position of the first space character preceeding the @ found above.
- Extract the text between those two positions
- ...
infousing RegExp.lastMatch and proceed to construct the response email. How does this work?
\binstructs the regex engine to look for a pattern starting at a word boundary[A-Za-z0-9.]+instructs the engine to find one or more repetitions of the alphanumeric and . characters.(?=@)defines a lookahead pattern. It is an instruction to perform a match for the preceeding pattern only if that pattern is followed by the @ character.
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