因此对非汉字(^\u4e00-\u9fa5)和非数字(^\u0030-\u0039)全部用replace函数替换即可,重点在于如何将unicode编码范围进行拼接。
import re str1 = " 212、Python用replace()函数删除制定 符号 " str2 = re.sub('([^\u4e00-\u9fa5\u0030-\u0039])', '', str1) print(str2) # "212用函数删除制定符号"
因此对非汉字(^\u4e00-\u9fa5)和非数字(^\u0030-\u0039)全部用replace函数替换即可,重点在于如何将unicode编码范围进行拼接。
import re str1 = " 212、Python用replace()函数删除制定 符号 " str2 = re.sub('([^\u4e00-\u9fa5\u0030-\u0039])', '', str1) print(str2) # "212用函数删除制定符号"