删除未知的特殊ch

2024-09-29 06:35:32 发布

您现在位置:Python中文网/ 问答频道 /正文

删除特殊字符

 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

但是我不能得到它

我想删除和维护空间和英语


Tags: 苹果reapplenew空间printza特殊字符
2条回答

你想要“苹果新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

相关问题 更多 >