Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如何通过掩码过滤字符串中的符号?
例如,我有简单的字符串:
"tes!@#$%^&*(())___+t" "test1" "test3N"
例如,如何删除不在“a-zA-Z”中的符号?
简单,在正则表达式中使用否定字符类:
import re re.sub('[^a-zA-Z]', '', inputstring)
[....]表示一个字符类。通常,班级中的任何内容都匹配。通过在开始时添加^插入符号,您可以否定课程;任何不在课堂上的东西都匹配。
[....]
^
结果:
>>> import re >>> re.sub('[^a-zA-Z]', '', '"tes!@#$%^&*(())___+t" "test1" "test3N"') 'testtesttestN'