我re在Python 3.6.5中遇到了模块问题.我的正则表达式中有这种模式:
'\\nRevision: (\d+)\\n'
Run Code Online (Sandbox Code Playgroud)
但是当我运行它时,我得到了一个DeprecationWarning.
我试图用一个反斜杠替换所有双反斜杠.我想用'class = \"highlight'替换'class = \\'highlight'.我认为python将'\\'视为一个反斜杠,将r'\\ +'视为带有两个反斜杠的字符串(这些是带有SO转义的3和4个反斜杠).但是,当我尝试
In [5]: re.sub(r'\\+', '\\', string)
sre_constants.error: bogus escape (end of line)
Run Code Online (Sandbox Code Playgroud)
所以我尝试用原始字符串切换替换字符串:
In [6]: re.sub(r'\\+', r'\\', string)
Out [6]: 'class=\\"highlight'
Run Code Online (Sandbox Code Playgroud)
这不是我需要的.所以我在原始字符串中只尝试了一个反斜杠:
In [7]: re.sub(r'\\+', r'\', string)
SyntaxError: EOL while scanning string literal
Run Code Online (Sandbox Code Playgroud)