>>> 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\"
|