This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: re slashes
Type: Stage: resolved
Components: Regular Expressions Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ezio.melotti, mrabarnett, serhiy.storchaka, Сергей Снегирёв
Priority: normal Keywords:

Created on 2016-12-19 15:38 by Сергей Снегирёв, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (2)
msg283626 - (view) Author: Сергей Снегирёв (Сергей Снегирёв) Date: 2016-12-19 15:38
>>> path
'd:/\\temp\\\\'
>>> pat = '[{}]+'.format(re.escape('\\/'))
>>> re.sub(pat, '\\', path)
Traceback (most recent call last):
  File "<pyshell#78>", line 1, in <module>
    re.sub(pat, '\\', path)
  File "C:\Users\Сергей\AppData\Local\Programs\Python\Python35\lib\re.py", line 182, in sub
    return _compile(pattern, flags).sub(repl, string, count)
  File "C:\Users\Сергей\AppData\Local\Programs\Python\Python35\lib\re.py", line 325, in _subx
    template = _compile_repl(template, pattern)
  File "C:\Users\Сергей\AppData\Local\Programs\Python\Python35\lib\re.py", line 312, in _compile_repl
    p = sre_parse.parse_template(repl, pattern)
  File "C:\Users\Сергей\AppData\Local\Programs\Python\Python35\lib\sre_parse.py", line 849, in parse_template
    s = Tokenizer(source)
  File "C:\Users\Сергей\AppData\Local\Programs\Python\Python35\lib\sre_parse.py", line 225, in __init__
    self.__next()
  File "C:\Users\Сергей\AppData\Local\Programs\Python\Python35\lib\sre_parse.py", line 239, in __next
    self.string, len(self.string) - 1) from None
sre_constants.error: bad escape (end of pattern) at position 0
>>> pat
'[\\\\\\/]+'
>>> 

In JS it works:

> 'd:/\\temp\\\\'.replace(new RegExp('[\\\\\\/]+', 'g'), '\\')
"d:\temp\"
msg283628 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-12-19 16:02
There is a problem with your replacement template. Python string literal '\\' is Python string containing a single backslash character. But backslash has special meaning in a replacement template, it starts escapes and backreferences. For using a literal backslash, it should be escaped: r'\\' or '\\\\'.

>>> re.sub(pat, r'\\', path)
'd:\\temp\\'
History
Date User Action Args
2022-04-11 14:58:40adminsetgithub: 73201
2016-12-19 16:02:01serhiy.storchakasetstatus: open -> closed

nosy: + serhiy.storchaka
messages: + msg283628

resolution: not a bug
stage: resolved
2016-12-19 15:38:56Сергей Снегирёвcreate