Lookahead and Lookbehind

2 min read ·

Lookahead and lookbehind are advanced regex features used to match patterns based on context without including that context in the result.

Positive Lookahead (?= )

Matches a pattern only if it is followed by another pattern.
Matches:
  • 100
  • 300

Negative Lookahead (?! )

Matches a pattern only if it is not followed by another pattern.
Matches:
  • 200

Note

Lookahead checks what comes after the pattern without including it in the result.


Positive Lookbehind (?<= )

Matches a pattern only if it is preceded by another pattern.
Matches:
  • 100
  • 200

Negative Lookbehind (?<! )

Matches a pattern only if it is not preceded by another pattern.
Matches:
  • 200

Pro Tip

Lookbehind is useful when extracting values that depend on a prefix.


Important Understanding

  • (?= ) checks forward condition
  • (?! ) checks forward negative condition
  • (?<= ) checks backward condition
  • (?<! ) checks backward negative condition
  • These do not include the condition in final result

Caution

Lookbehind patterns must have fixed length in Python.


Exercise

  • Extract numbers followed by "kg"
  • Extract numbers not followed by "kg"
  • Extract numbers after "$"
  • Extract numbers not preceded by "$"