【发布时间】:2021-06-08 13:23:42
【问题描述】:
为什么我从我的正则表达式搜索和替换代码中得到这个反斜杠? 如果我不使用任何特殊字符,我将不会得到它们。
问题是如何去掉这个反斜杠?
结果:
[bash]$ python /home/lucki1000/ESP/sr.py |grep "const char pass"
const char pass[] = "TESTpassword\.\-2"
我的预期:
const char pass[] = "TESTpassword.-2"
我的代码:
import re
replace_string = "TESTpassword.-2"
fname = "/home/lucki1000/ESP/adv.txt"
with open(fname, 'r+') as f:
text = f.read()
text = re.sub(r'(const char pass\[\] = \").*(\")', r'\1' + re.escape(replace_string) + r'\2', text)
f.seek(0)
print(text)
f.write(text)
f.truncate()
如果需要:
Arch linux(5.11.4-arch1-1 x64)
Python 3.9.2
【问题讨论】:
标签: python python-3.x regexp-replace