[New-bugs-announce] [issue47023] re.sub shows key error on regex escape chars provided in repl param

Siddhesh Sathe report at bugs.python.org
Tue Mar 15 04:17:57 EDT 2022


New submission from Siddhesh Sathe <siddheshsathe at rediffmail.com>:

Python 3.9.10 (main, Jan 15 2022, 18:56:52)
[GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> re.sub(r"{\w}", "\s", "Hello! {user}")
Traceback (most recent call last):
  File "/usr/lib/python3.9/sre_parse.py", line 1039, in parse_template
    this = chr(ESCAPES[this][1])
KeyError: '\\s'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.9/re.py", line 210, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "/usr/lib/python3.9/re.py", line 327, in _subx
    template = _compile_repl(template, pattern)
  File "/usr/lib/python3.9/re.py", line 318, in _compile_repl
    return sre_parse.parse_template(repl, pattern)
  File "/usr/lib/python3.9/sre_parse.py", line 1042, in parse_template
    raise s.error('bad escape %s' % this, len(this))
re.error: bad escape \s at position 0
>>>

----------
components: Regular Expressions
messages: 415225
nosy: ezio.melotti, mrabarnett, siddheshsathe
priority: normal
severity: normal
status: open
title: re.sub shows key error on regex escape chars provided in repl param
type: enhancement
versions: Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue47023>
_______________________________________


More information about the New-bugs-announce mailing list