【发布时间】:2016-12-24 08:22:53
【问题描述】:
对于像这样的句子:
sent = "This i$s a s[[]ample sentence.\nAnd another <<one>>.
\nMoreover, it is 'filtered'!"
我想得到:
"This is a sample sentence. And another one. Moreover, it is filtered."
因此,我认为使用re.sub 应该是要走的路。然而,RegEx 并没有按预期工作(就像它几乎总是这样^^)。
我的想法是使用\W 匹配每个非单词,然后排除[.,;!?] 以保留标点符号。我尝试的最后一个正则表达式是:
re.sub(r"(\W[^\.\,\;\?\!])", "", sent)
不幸的是,[^\.\,\;\?\!] 确实匹配不包含[.,;!?] 条目的任何内容,而不是简单地说“不要不按字面意思匹配这些字符”。
如何从匹配中排除这些字符?
【问题讨论】:
标签: python regex python-3.x replace