这是我正在运行的代码:
import re
FIND_TERM = r'C:\\Program Files\\Microsoft SQL Server\\90\\DTS\\Binn\\DTExec\.exe'
rfind_term = re.compile(FIND_TERM,re.I)
REPLACE_TERM = 'C:\\Program Files\\Microsoft SQL Server\\100\\DTS\\Binn\\DTExec.exe'
test = r'something C:\Program Files\Microsoft SQL Server\90\DTS\Binn\DTExec.exe something'
print rfind_term.sub(REPLACE_TERM,test)
我得到的结果是:
something C:\Program Files\Microsoft SQL Server@\DTS\Binn\DTExec.exe something
为什么会有@符号?
答案 0 :(得分:2)
你正在混合原始(r'')和普通字符串。
>>> FIND_TERM = r'C:\\Program Files\\Microsoft SQL Server\\90\\DTS\\Binn\\DTExec\.exe'
>>> REPLACE_TERM = r'C:\\Program Files\\Microsoft SQL Server\\100\\DTS\\Binn\\DTExec.exe'
>>> rfind_term = re.compile(FIND_TERM,re.I)
>>> test = r'something C:\Program Files\Microsoft SQL Server\90\DTS\Binn\DTExec.exe something'
>>> print rfind_term.sub(REPLACE_TERM,test)
something C:\Program Files\Microsoft SQL Server\100\DTS\Binn\DTExec.exe something
答案 1 :(得分:1)
RE引擎将\100
中的REPLACE_TERM
视为八进制转义码。您需要转义反斜杠,以便根据需要对其进行处理。