Skip to content

gh-122055: Clarify documentation for empty matches in RE #133169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Doc/library/re.rst
Original file line number Diff line number Diff line change
Expand Up @@ -985,8 +985,8 @@ Functions
That way, separator components are always found at the same relative
indices within the result list.

Empty matches for the pattern split the string only when not adjacent
to a previous empty match.
Adjacent empty matches are not possible, but an empty match can occur
immediately after a non-empty match.

.. code:: pycon

Expand Down Expand Up @@ -1089,9 +1089,12 @@ Functions

The optional argument *count* is the maximum number of pattern occurrences to be
replaced; *count* must be a non-negative integer. If omitted or zero, all
occurrences will be replaced. Empty matches for the pattern are replaced only
when not adjacent to a previous empty match, so ``sub('x*', '-', 'abxd')`` returns
``'-a-b--d-'``.
occurrences will be replaced.

Adjacent empty matches are not possible, but an empty match can occur
immediately after a non-empty match.
As a result, ``sub('x*', '-', 'abxd')`` returns ``'-a-b--d-'``
instead of ``'-a-b-d-'``.

.. index:: single: \g; in regular expressions

Expand Down Expand Up @@ -1122,8 +1125,7 @@ Functions
.. versionchanged:: 3.7
Unknown escapes in *repl* consisting of ``'\'`` and an ASCII letter
now are errors.
Empty matches for the pattern are replaced when adjacent to a previous
non-empty match.
An empty match can occur immediately after a non-empty match.

.. versionchanged:: 3.12
Group *id* can only contain ASCII digits.
Expand Down
Loading