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
  1. Locate the position of the @ character.
  2. Locate the position of the first space character preceeding the @ found above.
  3. Extract the text between those two positions
  4. ...
A faster and more elegant solution is possible by using regexes. The pattern \b[A-Za-z0-9.]+(?=@) would correctly match "For more info contact us at info@explainth.at". With the match done you could retrieve the name info using RegExp.lastMatch and proceed to construct the response email. How does this work?
  • \b instructs 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.
What if you want to skip free email addresses such as gmail, yahoo and hotmail? This can be done thanks to the fact that a lookahead pattern can contain any valid regex pattern - even another lookahead, as explained here.
Regular Expression Sandbox
Model
Data

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
Jump To...

Colophon