[New-bugs-announce] [issue42668] re.escape does not correctly escape newlines

Martin Altmayer report at bugs.python.org
Thu Dec 17 07:00:52 EST 2020


New submission from Martin Altmayer <martin.altmayer at web.de>:

re.escape('\n') returns '\\\n', i.e. a string consisting of a backslash and a newline. I believe it should return '\\n', i.e. a backslash and an 'n'. If the escape-result still contains a verbatim newline, why escape this character at all?

Note that Python's regular expressions engine allows newlines, so re.match(re.escape('\n'), '\n') gives a match. Thus, while this looks like an undesired behavior, it is not functionally broken.

The same problem applies to some other characters: \t\r\v\f

----------
components: Regular Expressions
files: test.py
messages: 383237
nosy: MartinAltmayer, ezio.melotti, mrabarnett
priority: normal
severity: normal
status: open
title: re.escape does not correctly escape newlines
type: behavior
versions: Python 3.10, Python 3.7, Python 3.8, Python 3.9
Added file: https://bugs.python.org/file49689/test.py

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


More information about the New-bugs-announce mailing list