#\d 匹配一个数字字符。等价于 [0-9] #\D 匹配一个非数字字符。等价于 [^0-9] #提取汉字 import re string = "hello,world!!%[545]你好234asd完全额。。。" str = re.sub("[A-Za-z0-9\!\%\[\]\,\。]", "", string) print(str) #从字符串中提取数字 totalCount = '100abcdef' totalCount = re.sub("\D", "", totalCount) print(totalCount) #提取字母字符串 import re string = "hello,world!!%[545]爱迪生234世界。。。" result = ''.join(re.findall(r'[A-Za-z]', string)) print(result)
来源:https://www.cnblogs.com/yuanmingzhou/p/11132566.html