python - 正则表达式不会子字符 '\\'
问题描述
我想从我的字符串中删除字符 \\。我试过 regextester 并且它匹配,https: //regex101.com/r/euGZsQ/1
s = '''That\\'s not true. There are a range of causes. There are a range of issues that we have to address. First of all, we have to identify, share and accept that there is a problem and that we\\''''
pattern = re.compile(r'\\{2,}')
re.sub(pattern, '', s)
我希望 sub 方法用什么来替换我的 \\ 来清理我的字符串。
解决方案
问题是您的字符串本身未标记为原始字符串。因此,第一个\
实际上逃脱了第二个。
观察:
import re
pattern = re.compile(r'\\{2,}')
s = r'''That\\'s not true. There are a range of causes. There are a range of issues that we have to address. First of all, we have to identify, share and accept that there is a problem and that we\\'''
re.sub(pattern, '', s)
输出:
"That's not true. There are a range of causes. There are a range of issues that we have to address. First of all, we have to identify, share and accept that there is a problem and that we"
推荐阅读
- python - 使用自定义配置启动 jupyter 时找不到其他内核
- python-3.x - 我将如何制作一个在使用时更改用户昵称的命令?(discord.py 重写)
- c# - 自动填充检查器数组
- python - python和swig版本兼容性问题
- python - python中的进度条用于监控复制任务
- java - 如何在 XML 中编写段落并使用 Java 代码?
- python - 使用pyspark检查数据框的所有列中是否存在大于零的值
- file - 测试文件上传时如何修复 tsc-watch 重启?
- python - Flask BuildError - 无法为端点构建 URL
- multithreading - cancel 和 uninterruptibleCancel 之间的区别(来自 Async 库)