python正則表達(dá)式re.match()匹配多個(gè)字符方法的實(shí)現(xiàn)
1. *表示匹配任意多個(gè)字符 \d*表示匹配任意多個(gè)數(shù)字字符
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é)果為
123
123Process finished with exit code 0
示例2:*
需求:匹配出,一個(gè)字符串第一個(gè)字母為大小字符,后面都是小寫(xiě)字母并且這些小寫(xiě)字母可有可無(wú)
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é)果
M
Aa
Aabcdef
2. +表示匹配1個(gè)或者多個(gè)任意字符 \w+表示匹配一個(gè)或多個(gè)字母,數(shù)字或下劃線(xiàn)
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é)果為
he
Hello
NoneProcess finished with exit code 0
示例2:+
需求:匹配出,變量名是否有效
import re names = ["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ù)字或下劃線(xiàn)
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é)果為
h
HProcess finished with exit code 0
示例2:?
需求:匹配出0到99之間的數(shù)字
import re ret = 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é)果
7
33
0 # 這個(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é)果為
he
Hel
hellProcess finished with exit code 0
示例2:{m}
需求:匹配出,8到20位的密碼,可以是大小寫(xiě)英文字母、數(shù)字、下劃線(xiàn)
import re ret = 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é)果
123a3g
1ad3123456addfcasdef
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)
hello
Hellowor
NoneProcess finished with exit code 0
到此這篇關(guān)于python正則表達(dá)式re.match()匹配多個(gè)字符方法的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python re.match()匹配字符內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python爬蟲(chóng)常見(jiàn)錯(cuò)誤集合
這篇文章主要介紹了python爬蟲(chóng)常見(jiàn)錯(cuò)誤,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02python二進(jìn)制轉(zhuǎn)換模塊的具體用法
在pyton中,通過(guò)struct模塊來(lái)對(duì)二進(jìn)制進(jìn)行轉(zhuǎn)換,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01python日期與時(shí)間戳的各種轉(zhuǎn)換示例
這篇文章主要介紹了python日期與時(shí)間戳的各種轉(zhuǎn)換示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02基于Python的網(wǎng)頁(yè)自動(dòng)化工具DrissionPage的使用詳解
DrissionPage 是一個(gè)基于 python 的網(wǎng)頁(yè)自動(dòng)化工具,它既能控制瀏覽器,也能收發(fā)數(shù)據(jù)包,還能把兩者合而為一,下面就跟隨小編一起來(lái)學(xué)習(xí)一下它的具體使用吧2024-01-01使用Python正則表達(dá)式操作文本數(shù)據(jù)的方法
這篇文章主要介紹了使用Python正則表達(dá)式操作文本數(shù)據(jù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05pandas如何將dataframe中的NaN替換成None
這篇文章主要介紹了pandas如何將dataframe中的NaN替換成None問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08