From 82a49aec7d8d553e9a9f5da6f2d4cfd290f93025 Mon Sep 17 00:00:00 2001 From: Alexey Semenyuk Date: Sun, 13 Jul 2025 14:08:02 +0500 Subject: [PATCH] gh-136618: lookbehind assertions doc at regex --- Doc/howto/regex.rst | 68 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/Doc/howto/regex.rst b/Doc/howto/regex.rst index 7486a378dbb06f..10952eb8e7f67c 100644 --- a/Doc/howto/regex.rst +++ b/Doc/howto/regex.rst @@ -7,7 +7,6 @@ :Author: A.M. Kuchling .. TODO: - Document lookbehind assertions Better way of displaying a RE, a string, and what it matches Mention optional argument to match.groups() Unicode (at least a reference) @@ -1061,6 +1060,73 @@ end in either ``bat`` or ``exe``: ``.*[.](?!bat$|exe$)[^.]*$`` +Lookbehind Assertions +--------------------- + +Lookbehind assertions work similarly to lookahead assertions, but they look +backwards in the string instead of forwards. They are available in both +positive and negative form, and look like this: + +``(?<=...)`` + Positive lookbehind assertion. This succeeds if the contained regular + expression, represented here by ``...``, successfully matches ending at the + current location, and fails otherwise. The matching engine doesn't advance; + the rest of the pattern is tried right where the assertion started. + +``(?