mirror of
https://github.com/python/cpython.git
synced 2025-08-31 22:18:28 +00:00
Issue 1519638: Now unmatched groups are replaced with empty strings in re.sub()
and re.subn().
This commit is contained in:
parent
365e28238f
commit
7438e4b56f
5 changed files with 26 additions and 8 deletions
|
@ -880,14 +880,12 @@ def parse_template(source, pattern):
|
|||
|
||||
def expand_template(template, match):
|
||||
g = match.group
|
||||
sep = match.string[:0]
|
||||
empty = match.string[:0]
|
||||
groups, literals = template
|
||||
literals = literals[:]
|
||||
try:
|
||||
for index, group in groups:
|
||||
literals[index] = s = g(group)
|
||||
if s is None:
|
||||
raise error("unmatched group")
|
||||
literals[index] = g(group) or empty
|
||||
except IndexError:
|
||||
raise error("invalid group reference")
|
||||
return sep.join(literals)
|
||||
return empty.join(literals)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue