python re模塊記錄
阿新 • • 發佈:2017-05-25
findall else 包括 none 第一個 port bsp search re.search
import re
‘‘‘
re模塊
compile
match search findall
group groups
正則表達式常用格式:
字符:\d \w \t .
(\d:數字;\w:字母數字下劃線_;\t:制表符;點.:處了回車外的所有字符)
次數:* + ? {m} {m,n}
(+:>=1數字;*:>=0個字符;?:0或1,{m}次,{m,n}範圍,包括n)
match字符串開頭開始匹配,第一個不匹配就返回none
search一次尋找整個整個字符串,直到匹配為止,只返回一個匹配值
findall尋找字符串的所有,遍歷整個字符串,返回所有相匹配的值
‘‘‘
#match
res1 = re.match(‘\d+‘, ‘wqe221111wd3345‘)
if res1:
print res1.group()
else:
print ‘nothing‘
#search
res2 = re.search(‘\d+‘,‘wqe221111wd3345‘)
if res2:
print res2.group()
else:
print ‘nothing‘
#findall
res3 = re.findall(‘\d+‘,‘wqe221111wd3345‘)
print res3
#compile
com = re.compile(‘\d+‘,)
print com.findall(‘wqe221111wd3345‘)
#group groups
res4 = re.search(‘(\d+)\w*(\d+)‘,‘wqe221111wd3345‘)
print res4.group()
print res4.groups()
python re模塊記錄