diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2018-01-04 09:06:13 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-04 09:06:13 (GMT) |
commit | fbb490fd2f38bd817d99c20c05121ad0168a38ee (patch) | |
tree | 417d39bc824e6e62503f94033dbd2b37a8b5545b /Doc/library | |
parent | 0cc99c8cd70d422e4b345837a907db30e9180ab9 (diff) | |
download | cpython-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.rst | 14 |
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) |