我必须清除一个字符串删除特殊符号/#$%^&*@0123456789
,只有当它们被不在列表中的字母或符号分开时。
示例:
H8e%&l6&%l@8095o a@/9^65$n228d w%e60$$#&9l3@/c6o5m3e --> Hello and welcome
I1^/0^^@9t #$%% i/@4#s 11P17/9$M 5^&* a^$45$5$0n&##^4d 6^&&* I $%^$%^ a8@@94%3*m t3120i36&^1r2&^##0e&^d ---> It #$%% is 11PM 5^&* and 6^&&* I $%^$%^ am tired
,. a3%2%1/3$s*0. d8^! -->,. as. d!
##%12Symbols on the left must remain untouched --> ##%12Symbols on the left must remain untouched
我发现可以使用re.sub
:
import re
def _correct_message(message):
new_final_string = re.sub("(?<=[a-zA-Z\.\!])[/#\$\%\^\&\*\@0123456789]+(?=[a-zA-Z\.\!])", '', message)
return new_final_string
但是我不喜欢这样一个事实:我必须手动添加列表中没有的符号。有没有可能在没有regex
的情况下实现呢?你知道吗
这是我能得到的最接近的:
用空格替换所有匹配项
相关问题 更多 >
编程相关推荐