python提取字符串中的中文、数字、字母

转载自:https://blog.csdn.net/luoganttcc/article/details/80946194

#\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

猜你喜欢

转载自blog.csdn.net/weixin_35757704/article/details/108365303
今日推荐