python正則表達(dá)式re.match()匹配多個(gè)字符方法的實(shí)現(xiàn)
import re text = '123h1ello world'text1 = '123Hello world456'text2 = 'hello world' res = re.match('d*', text) res1 = re.match('d*', text1)res2 = re.match('d*', text2) print(res.group())print(res1.group())print(res2.group())
輸出結(jié)果為
123123
Process finished with exit code 0
示例2:*
需求:匹配出,一個(gè)字符串第一個(gè)字母為大小字符,后面都是小寫字母并且這些小寫字母可有可無
import re#注意是前一個(gè)字符ret = re.match('[A-Z][a-z]*','M')print(ret.group())ret = re.match('[A-Z][a-z]*','AaBcDE')print(ret.group())ret = re.match('[A-Z][a-z]*','Aabcdef')print(ret.group())
#運(yùn)行結(jié)果MAaAabcdef
2. +表示匹配1個(gè)或者多個(gè)任意字符 w+表示匹配一個(gè)或多個(gè)字母,數(shù)字或下劃線import re text = 'he+llo world'text1 = 'Hello world456'text2 = '+hello world' res = re.match('w+', text)res1 = re.match('w+', text1)res2 = re.match('w+', text2) print(res.group())print(res1.group())print(res2)
輸出結(jié)果為
heHelloNone
Process finished with exit code 0
示例2:+
需求:匹配出,變量名是否有效
import renames = ['name1','_name','2_name','__name__']for i in names: ret = re.match('[a-zA-Z_]+[w]*',i) if ret: print('變量名 %s 符合要求' % ret.group()) else: print('變量名 %s 非法' % i)
#運(yùn)行結(jié)果變量名 name1 符合要求變量名 _name 符合要求變量名 2_name 非法變量名 __name__ 符合要求
3. ?表示匹配0個(gè)或一個(gè)字符 w?表示匹配0或1個(gè)字母,數(shù)字或下劃線import re text = 'he+llo world'text1 = 'Hello world456'text2 = '+hello world' res = re.match('w?', text)res1 = re.match('w?', text1)res2 = re.match('w?', text2) print(res.group())print(res1.group())print(res2.group())
輸出結(jié)果為
hH
Process finished with exit code 0
示例2:?
需求:匹配出0到99之間的數(shù)字
import reret = re.match('[1-9]?[0-9]','7')print(ret.group())ret = re.match('[1-9]?d','33')print(ret.group())ret = re.match('[1-9]?d','09')print(ret.group())
#運(yùn)行結(jié)果7330 # 這個(gè)結(jié)果并不是想要的,利用$才能解決
4. {m}表示匹配m個(gè)字符 d{11}表示匹配11個(gè)數(shù)字字符import re text = 'he+llo world'text1 = 'Hello world456'text2 = 'hello world' res = re.match('w{2}', text)res1 = re.match('w{3}', text1)res2 = re.match('w{4}', text2) print(res.group())print(res1.group())print(res2.group())
輸出結(jié)果為
heHelhell
Process finished with exit code 0
示例2:{m}需求:匹配出,8到20位的密碼,可以是大小寫英文字母、數(shù)字、下劃線
import reret = re.match('[a-zA-Z0-9_]{6}','123a3g45678')print(ret.group())ret = re.match('[[a-zA-Z0-9_]{8,20}','1ad3123456addfcasdef')print(ret.group())
#運(yùn)行結(jié)果123a3g1ad3123456addfcasdef
5. {m,n}表示匹配m-n個(gè)字符 w{2,4}表示匹配2-4個(gè)字符import re text = 'hello world'text1 = 'Helloworld456'text2 = 'hello world' res = re.match('w{2,5}', text)res1 = re.match('w{6,8}', text1)res2 = re.match('w{20,25}', text2) print(res.group())print(res1.group())print(res2)
helloHelloworNone
Process finished with exit code 0
到此這篇關(guān)于python正則表達(dá)式re.match()匹配多個(gè)字符方法的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python re.match()匹配字符內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ajax post下載flask文件流以及中文文件名問題2. 網(wǎng)頁中img圖片使用css實(shí)現(xiàn)等比例自動(dòng)縮放不變形(代碼已測(cè)試)3. Python基于Serializer實(shí)現(xiàn)字段驗(yàn)證及序列化4. JSP之表單提交get和post的區(qū)別詳解及實(shí)例5. css進(jìn)階學(xué)習(xí) 選擇符6. Python安裝并操作redis實(shí)現(xiàn)流程詳解7. PHP字符串前后字符或空格刪除方法介紹8. html清除浮動(dòng)的6種方法示例9. JavaScript實(shí)現(xiàn)組件化和模塊化方法詳解10. 使用AJAX(包含正則表達(dá)式)驗(yàn)證用戶登錄的步驟
