您可以尝试删除所有非单词字符:
string_value = "hello ' how ' are - you ? and/ nice to % meet # you"
output = re.sub(r'\s+', ' ', re.sub(r'[^\w\s]+', '', string_value))
print(string_value)
print(output)
hello ' how ' are - you ? and/ nice to % meet # you
hello how are you and nice to meet you
我首先使用的解决方案针对使用模式的所有非单词字符(除了空白)
[^\w\s]+
. 但是,有可能留下两个或更多空间的集群。所以,我们再打电话给
re.sub
删除多余的空格。