我有这一行来删除除空格之外的所有非字母数字字符
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'
请您参考如下方法:
re.sub(r'[^A-Za-z0-9 ]+', '', s)
(编辑)澄清: [] 创建一个字符列表。 ^ 否定列表。 A-Za-z 是英文字母, 是空格。对于其中的任何一个或多个(即不是 A-Z、a-z 或空格的任何内容)替换为空字符串。
