【问题标题】:Regex will not sub characters '\\' [duplicate]正则表达式不会子字符'\\' [重复]
【发布时间】:2019-06-10 06:38:16
【问题描述】:

我想从我的字符串中删除字符 \\。我试过 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 方法用什么来替换我的 \\ 来清理我的字符串。

【问题讨论】:

  • 如果只想删除 '\\' 为什么不替换它呢? s.replace("\\\\", "")

标签: python regex


【解决方案1】:

问题是您的 字符串本身 没有被标记为原始字符串。因此,第一个 \ 实际上转义了第二个。

观察:

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"

【讨论】:

  • 很好用,可用时接受。但是,我的实际用例是从带有s = r' '.join(s) 的列表转换而来的字符串。转换为原始字符串并不能解决这个问题。
  • @franticoreo 你怎么知道你的字符串实际上有两个反斜杠?请注意,每个反斜杠将被打印两次。尝试从正则表达式中删除 {2,} 或仅使用 .replace('\\', '')
  • @franticoreo 请注意r''.join(...)''.join(...) 相同。 “原始字符串”仅具有 literal 的含义。
【解决方案2】:

你可以试试:

import re
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\\' '''
d = re.sub(r'\\', '', s)
print(d)

输出:

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'

【讨论】:

  • 请注意,这也将匹配单个反斜杠。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-21
  • 2018-09-07
相关资源
最近更新 更多