summaryrefslogtreecommitdiffstats
path: root/Doc/library
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-01-04 09:06:13 (GMT)
committerGitHub <noreply@github.com>2018-01-04 09:06:13 (GMT)
commitfbb490fd2f38bd817d99c20c05121ad0168a38ee (patch)
tree417d39bc824e6e62503f94033dbd2b37a8b5545b /Doc/library
parent0cc99c8cd70d422e4b345837a907db30e9180ab9 (diff)
downloadcpython-fbb490fd2f38bd817d99c20c05121ad0168a38ee.zip
cpython-fbb490fd2f38bd817d99c20c05121ad0168a38ee.tar.gz
cpython-fbb490fd2f38bd817d99c20c05121ad0168a38ee.tar.bz2
bpo-32308: Replace empty matches adjacent to a previous non-empty match in re.sub(). (#4846)
Diffstat (limited to 'Doc/library')
-rw-r--r--Doc/library/re.rst14
1 files changed, 10 insertions, 4 deletions
diff --git a/Doc/library/re.rst b/Doc/library/re.rst
index dae1d7e..9b175f4 100644
--- a/Doc/library/re.rst
+++ b/Doc/library/re.rst
@@ -708,12 +708,15 @@ form.
That way, separator components are always found at the same relative
indices within the result list.
- The pattern can match empty strings. ::
+ Empty matches for the pattern split the string only when not adjacent
+ to a previous empty match.
>>> re.split(r'\b', 'Words, words, words.')
['', 'Words', ', ', 'words', ', ', 'words', '.']
+ >>> re.split(r'\W*', '...words...')
+ ['', '', 'w', 'o', 'r', 'd', 's', '', '']
>>> re.split(r'(\W*)', '...words...')
- ['', '...', 'w', '', 'o', '', 'r', '', 'd', '', 's', '...', '']
+ ['', '...', '', '', 'w', '', 'o', '', 'r', '', 'd', '', 's', '...', '', '', '']
.. versionchanged:: 3.1
Added the optional flags argument.
@@ -778,8 +781,8 @@ form.
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 match, so ``sub('x*', '-', 'abc')`` returns
- ``'-a-b-c-'``.
+ when not adjacent to a previous empty match, so ``sub('x*', '-', 'abxd')`` returns
+ ``'-a-b--d-'``.
In string-type *repl* arguments, in addition to the character escapes and
backreferences described above,
@@ -805,6 +808,9 @@ form.
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.
+
.. function:: subn(pattern, repl, string, count=0, flags=0)