2024-09-29 06:35:32 发布
网友
删除特殊字符
s="____Ç_apple___ _______new A_____" print(re.sub('[^0-9a-zA-Z]\s+$', '', s))
结果=_____________;苹果__;新A __;
s="____Ç_apple___ _______new A_____" print(re.sub('[^0-9a-zA-Z]', '', s))
结果=applenewA
决赛 结果=苹果新A
但是我不能得到它
我想删除和维护空间和英语
你想要“苹果新A”作为结果,对吗
s="____Ç_apple___ _______new A_____" result = re.sub('[^a-zA-Z|\s]+', '', s) # apple new A result = ' '.join(result.split()) # apple new A print(result)
由于要将多个空格合并到一个空格中,然后删除非单词或空格的字符,因此应使用两个单独的正则表达式替换:
print(re.sub(r'[^0-9a-zA-Z ]+', '', re.sub(r'\s+', ' ', s)))
这将产生:
apple new A
你想要“苹果新A”作为结果,对吗
由于要将多个空格合并到一个空格中,然后删除非单词或空格的字符,因此应使用两个单独的正则表达式替换:
这将产生:
相关问题 更多 >
编程相关推荐