regex - 正则表达式查询:删除转义字符但保留字符串中的标点符号
问题描述
我有一个类似于 - 的字符串"\n\n\some text\t goes here. some\t\t other text goes here\b\n\n\n"
。
我想要的是 -"some text goes here. some other text goes here."
这是我正在做的事情:re.sub('[^A-Za-z0-9]+', ' ', s)
问题是这也会删除所有标点符号。我如何保留这些?
解决方案
这是一个解决方案,可以找到字符串中的所有转义字符,然后将其删除。
r = repr(s) # Convert escape sequences to literal backslashes
r = r[1:-1] # Remove the quote characters introduced by `repr`
escapes = set(re.findall(r'\\\w\d*', r)) # Get escape chars
answer = re.sub('|'.join(map(re.escape, escapes)), '', r) # Remove them
# \ome text goes here. some other text goes here
推荐阅读
- paypal - Paypal Express 结帐智能按钮阻止了带有 Origin 的框架
- android - Android 房间创建查看错误
- r - 将数据框列表转换为具有特定列名的单个数据框
- r - 如何通过在 R 中使用最大似然来估计线性回归?
- android - 使用来自 gradle 任务的 Android gradle 插件 dsl 设置参数
- ubuntu - ubuntu终端新目录中的链接数量问题
- ubuntu - Ubuntu 更新后 Intellij 一直冻结
- html - 奇怪的 WebRTC SinkID 问题
- html - 如何减少固定高度表格的两行之间的空间
- firebase - 验证 FCM 用户令牌的批量方法?