编程语言
首页 > 编程语言> > python 正则之提取字符串中的汉字,数字,字母

python 正则之提取字符串中的汉字,数字,字母

作者:互联网

#\d 匹配一个数字字符。等价于 [0-9]
#\D 匹配一个非数字字符。等价于 [^0-9]

#过滤字符串中的英文与符号,保留汉字
import re
st = "hello,world!!%[545]你好234世界。。。"
ste = re.sub("[A-Za-z0-9\!\%\[\]\,\。]", "", st)
print(ste)

#从字符串中提取数字
totalCount = '100abc'
totalCount = re.sub("\D", "", totalCount) 
print(totalCount)

#从字符串中提取字母字符串
import re
st = "hello,world!!%[545]你好234世界。。。"
result = ''.join(re.findall(r'[A-Za-z]', st)) 
print(result)
你好世界
100
helloworld

标签:数字,re,python,print,st,totalCount,正则,字符串
来源: https://blog.csdn.net/fangxiananvhai/article/details/95322590