如何在python中将``与regex匹配?

2024-10-01 07:36:46 发布

您现在位置:Python中文网/ 问答频道 /正文

tweets中有一个符号:

“@BrownieSWP: High is s***????” you like 12 tf

符号不是"。我写这个正则表达式来匹配它:

re.sub('(“|”)', '"', tweet)

这个正则表达式在崇高的文本中工作。但它在python中不起作用。你知道吗


Tags: 文本reyouistf符号tweetslike
2条回答

对我来说很管用

>>> s = r"“@BrownieSWP: High is s***????” you like 12 tf"
>>> m = re.sub(r'[”“]', r'', s)
>>> m
'@BrownieSWP: High is s***???? you like 12 tf'

复制/粘贴的字符是U+201C“左双引号”。在re.sub()中也有相应的右引号U+201D。可能您尝试粘贴它的环境没有设置为正确处理Unicode,并将其转换为其他编码。(另见How do I see the current encoding of a file in Sublime Text 2?

始终可以使用Python的转义码来明确地和ASCII兼容地引用Unicode字符;re.sub(u'[\u201c\u201d]', '', tweet)

相关问题 更多 >