Lookahead and Lookbehind

Download regex eBook

Syntax

  • Positive lookahead: (?=pattern)
  • Negative lookahead: (?!pattern)
  • Positive lookbehind: (?<=pattern)
  • Negative lookbehind: (?<!pattern)

Remarks

Not supported by all regex engines.

Additionally, many regex engines limit the patterns inside lookbehinds to fixed-length strings. For example the pattern (?<=a+)b should match the b in aaab but throws an error in Python.

Capturing groups are allowed and work as expected, including backreferences. The lookahead/lookbehind itself is not a capturing group, however.

Related Examples

Stats

404 Contributors: 5
Tuesday, December 6, 2016
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook