Python:正则表达式在Windows中匹配,但在Linux 4.4.59+中不匹配



我有这段代码,试图用直引号替换弯引号

quoteChars = [u'u2018', u'u2019']
pattern = u'({0})'.format('|'.join(quoteChars))
matched = re.search(pattern, myString)  # match against whole string
if matched:
self.log('SELF:: Search Query:: Replacing characters in string. Found one of these {0}'.format(pattern))
myString = re.sub(pattern, "'", myString)
self.log('SELF:: Amended Search Query [{0}]'.format(myString))
else:
self.log('SELF:: Search Query:: String has none of these {0}'.format(pattern))

我将变量 myString 设置为以下(‘Pop‑Up’ Edition)在Windows中,它正确地检测到有卷曲的撇号,但是当我在Mac上尝试将其操作系统报告为Linux 4.4.59 +时,它与模式不匹配。

我是否必须在 Linux 上以不同的方式设置正则表达式模式?规则是什么?关于单撇号和双撇号,开始还是关闭?

我会使用正则表达式转义:

quoteChars = [r'u2018', r'u2019']
pattern = '|'.join(quoteChars)

然后

myString = re.sub(pattern, "'", myString)

相关内容

最新更新