我有这一行删除除空格外的所有非字母数字字符
re.sub(r'\W+', '', s)
虽然,它仍然保留非英文字符。
例如,如果我有
re.sub(r'\W+', '', 'This is a sentence, and here are non-english 托利 苏 !!11')
我想获得输出:
> 'This is a sentence and here are non-english 11'
原文由 Filipe Aleixo 发布,翻译遵循 CC BY-SA 4.0 许可协议
(编辑)澄清:
[]
创建一个字符列表。^
否定列表。A-Za-z
是英文字母和 “ 是空间。对于其中的任何一个或多个(即不是 AZ、az 或空格的任何内容)替换为空字符串。