gh-151819: Clarify backtracking semantics of (?(id/name)yes|no) in re docs (gh-151819)#151913
Open
Dodothereal wants to merge 1 commit into
Open
gh-151819: Clarify backtracking semantics of (?(id/name)yes|no) in re docs (gh-151819)#151913Dodothereal wants to merge 1 commit into
(?(id/name)yes|no) in re docs (gh-151819)#151913Dodothereal wants to merge 1 commit into
Conversation
…ongh-151819) Closes python#151819 The (?(id/name)yes-pattern|no-pattern) documentation claims an example pattern (<)?(\w+@\w+(?:\.\w+)+)(?(1)>|$) will not match '<user@host.com'. In fact the engine matches 'user@host.com' because the leading < capture group is rerolled when the yes-pattern cannot consume the trailing >. The same backtracking behaviour occurs in simpler cases such as (<)?\w+(?(1)>) matching only '3' from '<3'. This change documents the backtracking semantics explicitly and corrects the embedded example. Adds a regression test that locks in the visible behavior.
|
The following commit authors need to sign the Contributor License Agreement: |
Documentation build overview
|
(?(id/name)yes|no) in re docs (gh-151819)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #151819
Summary
The :py:doc:
re-syntaxdocumentation for(?(id/name)yes-pattern|no-pattern)contains two issues:
The documented example pattern
(<)?(\w+@\w+(?:\.\w+)+)(?(1)>|$)is claimed to "not match with'<user@host.com'". That is not what the regex engine does. Withthat input the engine first consumes
<to satisfy group 1,cannot match the closing
>, and backtracks. Backtracking dropsthe
<capture and the engine then matches'user@host.com'starting at position 1, with
group(1) is None.The general interaction between the conditional construct and
backtracking is undocumented. The simplest illustration is
re.search('(<)?\\w+(?(1)>)', '<3'): it returnsmatch='3'atspan=(1, 2)withgroup(1) is None.This PR corrects the email example wording and adds a paragraph that
documents the backtracking-clears-the-capture behavior, plus a
regression test that locks in the visible behavior across the
relevant cases.
Test plan
ReTests.test_re_conditional_drops_capture_on_backtrackinLib/test/test_re.pyexercises the issue reproducer and severalclosely related inputs. All four assertions were verified locally
against the regex engine; the test will be run as part of the CPython
CI test suite for this PR.
AI assistance
This patch was drafted with the help of an AI coding assistant. The
diff was reviewed line by line before submission and the regex
behavior was verified independently against CPython's
reengine.Per the CPython AI policy this disclosure is offered; it is not
required.