Regular Expressions Basics


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