Regular Expressions Lookahead and Lookbehind Basics

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Example

A positive lookahead (?=123) asserts the text is followed by the given pattern, without including the pattern in the match. Similarly, a positive lookbehind (?<=123) asserts the text is preceded by the given pattern. Replacing the = with ! negates the assertion.


Input: 123456

  • 123(?=456) matches 123 (positive lookahead)
  • (?<=123)456 matches 456 (positive lookbehind)
  • 123(?!456) fails (negative lookahead)
  • (?<!123)456 fails (negative lookbehind)

Input: 456

  • 123(?=456) fails
  • (?<=123)456 fails
  • 123(?!456) fails
  • (?<!123)456 matches 456


Got any Regular Expressions Question?