Skip to content

Commit 55a7cb1

Browse files
[3.13] gh-122055: Clarify documentation for empty matches in RE (GH-133169) (GH-134218)
(cherry picked from commit 44b73d3) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 8d92823 commit 55a7cb1

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

Doc/library/re.rst

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,8 @@ Functions
988988
That way, separator components are always found at the same relative
989989
indices within the result list.
990990

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

994994
.. code:: pycon
995995
@@ -1092,9 +1092,12 @@ Functions
10921092

10931093
The optional argument *count* is the maximum number of pattern occurrences to be
10941094
replaced; *count* must be a non-negative integer. If omitted or zero, all
1095-
occurrences will be replaced. Empty matches for the pattern are replaced only
1096-
when not adjacent to a previous empty match, so ``sub('x*', '-', 'abxd')`` returns
1097-
``'-a-b--d-'``.
1095+
occurrences will be replaced.
1096+
1097+
Adjacent empty matches are not possible, but an empty match can occur
1098+
immediately after a non-empty match.
1099+
As a result, ``sub('x*', '-', 'abxd')`` returns ``'-a-b--d-'``
1100+
instead of ``'-a-b-d-'``.
10981101

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

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

11311133
.. versionchanged:: 3.12
11321134
Group *id* can only contain ASCII digits.

0 commit comments

Comments
 (0)