All posts
April 30, 20265 min read

Regex lookahead and lookbehind, explained simply

Lookarounds are the regex feature people avoid for years and then cannot live without. They answer a need that comes up constantly: match this, but only if it is next to that, without including “that” in the match.

The four types

  • (?=...) lookahead: the match must be followed by this.
  • (?!...) negative lookahead: the match must not be followed by this.
  • (?<=...) lookbehind: the match must be preceded by this.
  • (?<!...) negative lookbehind: the match must not be preceded by this.

The key idea: lookarounds are conditions, not captures. They check the neighborhood of a position without consuming any characters, so what you match stays clean.

Real examples

Get a price without the currency symbol. (?<=\$)\d+(\.\d+)? matches 42.50 in $42.50. The dollar sign must be there, but it is not part of the match, so no cleanup is needed afterward.

Password rules. ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d).{8,}$requires a lowercase letter, an uppercase letter, and a digit, in any order, in a string at least 8 long. Each lookahead scans independently from the start, which is how the “any order” part works.

Match a word not followed by another. error(?! handled) finds error except where the text says error handled.

Test these patterns liveTest regular expressions against sample text with live match highlighting and capture groups.

When not to use them

Lookarounds make patterns harder to read, and stacking several of them usually means a regex is doing a parser's job. If you find yourself writing three or more lookarounds, consider matching more loosely and filtering in code, where the logic has names and can be tested. Regex is best at finding shapes in text, not at enforcing business rules.