Python正則re模塊使用步驟及原理解析
python中使用正則表達(dá)式的步驟:
1.導(dǎo)入re模塊:import re
2.初始化一個(gè)Regex對(duì)象:re.compile()
3.剛剛創(chuàng)建的Regex對(duì)象調(diào)用search方法進(jìn)行匹配,返回要給March對(duì)象
4.剛剛的March對(duì)象調(diào)用group方法,展示匹配到的字符串
下面例子的知識(shí)點(diǎn):
對(duì)正則表達(dá)式分組用:(),正則里的分組計(jì)數(shù)從1開(kāi)始,不是從0,切記~~
- group(數(shù)字):去對(duì)應(yīng)的分組的值
- groups():返回所有分組的元組形式
\d表示一個(gè)數(shù)字
regex_obj = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)')
match_obj = regex_obj.search('我司電話(huà):035-411-1234')
result1 = match_obj.group(1)
result2 = match_obj.group(2)
result3 = match_obj.group(3)
print(result1)
print(result2)
print(result3)
result4 = match_obj.group()
print(result4)
result5 = match_obj.groups()
print(result5)
執(zhí)行結(jié)果:
035
411
1234
035-411-1234
('035', '411', '1234')
補(bǔ)充知識(shí)點(diǎn):\w表示一個(gè)單詞,\s表示一個(gè)空格
regex_obj = re.compile(r'(\d\w\d)-(\d\d\d)-(\d\d\d\d)')
match_obj = regex_obj.search('我司電話(huà):0a5-411-1234')
result = match_obj.group(1)
print(result)
regex_obj = re.compile(r'(\d\w\d)-(\d\d\d)-(\d\d\d\d)')
match_obj = regex_obj.search('我司電話(huà):0哈5-411-1234')
result = match_obj.group(1)
print(result)
regex_obj = re.compile(r'(\d\s\d)-(\d\d\d)-(\d\d\d\d)')
match_obj = regex_obj.search('我司電話(huà):0 5-411-1234')
result = match_obj.group(1)
print(result)
執(zhí)行結(jié)果:
0a5
0哈5
0 5
| 或:
regex_obj = re.compile(r'200|ok|successfully')
match_obj1 = regex_obj.search('vom get request and stored successfully')
result1 = match_obj1.group()
print(result1)
match_obj2 = regex_obj.search('vom get request,response 200 ok')
result2 = match_obj2.group()
print(result2)
match_obj3 = regex_obj.search('vom get request,response ok 200')
result3 = match_obj3.group()
print(result3)
執(zhí)行結(jié)果:
successfully
200
ok
注意:如果search返回的March對(duì)象只有一個(gè)結(jié)果值的話(huà),不能用groups,只能用group
regex_obj = re.compile(r'200|ok|successfully')
match_obj1 = regex_obj.search('vom get request and stored successfully')
result2 = match_obj1.groups()
print(result2)
result1 = match_obj1.group()
print(result1)
執(zhí)行結(jié)果:
()
successfully
? :可選匹配項(xiàng)
+ :1次 或 n次 匹配
* :*前面的字符或者字符串匹配 0次、n次
注意:*前面必須要有內(nèi)容
regex_obj = re.compile(r'(haha)*,welcome to vom_admin system') 指haha這個(gè)字符串匹配0次或者多次
regex_obj = re.compile(r'(ha*),welcome to vom_admin system') 指ha這個(gè)字符串匹配0次或者多次
. : 通配符,匹配任意一個(gè)字符
所以常常用的組合是:.*
regex_obj = re.compile(r'(.*),welcome to vom_admin system')
match_obj1 = regex_obj.search('Peter,welcome to vom_admin system')
name = match_obj1.group(1)
print(name)
執(zhí)行結(jié)果:
Peter
{} : 匹配特定的次數(shù)
里面只寫(xiě)一個(gè)數(shù)字:匹配等于數(shù)字的次數(shù)
里面寫(xiě){3,5}這樣兩個(gè)數(shù)字的,匹配3次 或 4次 或 5次,按貪心匹配法,能滿(mǎn)足5次的就輸出5次的,沒(méi)有5次就4次,4次也沒(méi)有才是3次
regex_obj = re.compile(r'((ha){3}),this is very funny')
match_obj1 = regex_obj.search('hahahaha,this is very funny')
print("{3}結(jié)果",match_obj1.group(1))
regex_obj = re.compile(r'((ha){3,5}),this is very funny')
match_obj1 = regex_obj.search('hahahaha,this is very funny')
print("{3,5}結(jié)果",match_obj1.group(1))
執(zhí)行結(jié)果:
{3}結(jié)果 hahaha
{3,5}結(jié)果 hahahaha
findall():返回所有匹配到的字串的列表
regex_obj = re.compile(r'\d\d\d')
match_obj = regex_obj.findall('我是101班的,小李是103班的')
print(match_obj)
regex_obj = re.compile(r'(\d\d\d)-(\d\d\d)-(\d\d\d\d)')
match_obj = regex_obj.findall('我家電話(huà)是123-123-1234,我公司電話(huà)是890-890-7890')
print(match_obj)
打印結(jié)果:
['101', '103']
[('123', '123', '1234'), ('890', '890', '7890')]
[]:創(chuàng)建自己的字符集:
[abc]:包括[]內(nèi)的字符
[^abc]:不包括[]內(nèi)的所有字符
也可以使用:[a-zA-Z0-9]這樣簡(jiǎn)寫(xiě)
regex_obj = re.compile(r'[!@#$%^&*()]') name = input("請(qǐng)輸入昵稱(chēng),不含特殊字符:") match_obj = regex_obj.search(name) if match_obj: print("昵稱(chēng)輸入不合法,包含了特殊字符:", match_obj.group()) else: print("昵稱(chēng)有效")
執(zhí)行結(jié)果:
請(qǐng)輸入昵稱(chēng),不含特殊字符:*h
昵稱(chēng)輸入不合法,包含了特殊字符: *
^:開(kāi)頭
$:結(jié)尾
regex_obj = re.compile(r'(^[A-Z])(.*)')
name = input("請(qǐng)輸入昵稱(chēng),開(kāi)頭必須大寫(xiě)字母:")
match_obj = regex_obj.search(name)
print(match_obj.group())
執(zhí)行結(jié)果:
請(qǐng)輸入昵稱(chēng),開(kāi)頭必須大寫(xiě)字母:A1234
A1234
sub():第一個(gè)參數(shù)為要替換成的,第二個(gè)參數(shù)傳被替換的,返回替換成功后的字符串
regex_obj = re.compile(r'[!@#$%^&*()]')
match_obj = regex_obj.sub('嘿','haha,$%^,hahah')
print(match_obj)
執(zhí)行結(jié)果:
haha,嘿嘿嘿,hahah
補(bǔ)充一下正則表達(dá)式的表,正則太復(fù)雜了,要常看常用才能熟練
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
淺談Django中的數(shù)據(jù)庫(kù)模型類(lèi)-models.py(一對(duì)一的關(guān)系)
今天小編就為大家分享一篇淺談Django中的數(shù)據(jù)庫(kù)模型類(lèi)-models.py(一對(duì)一的關(guān)系),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05VSCode基礎(chǔ)使用與VSCode調(diào)試python程序入門(mén)的圖文教程
這篇文章主要介紹了VSCode基礎(chǔ)使用+VSCode調(diào)試python程序入門(mén)圖文教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03淺談Python中的可變對(duì)象和不可變對(duì)象
下面小編就為大家?guī)?lái)一篇淺談Python中的可變對(duì)象和不可變對(duì)象。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07Python對(duì)小數(shù)進(jìn)行除法運(yùn)算的正確方法示例
這篇文章主要介紹了Python對(duì)小數(shù)進(jìn)行除法運(yùn)算的正確方法示例,正確的方法是需要轉(zhuǎn)換成浮點(diǎn)數(shù),否則永遠(yuǎn)不會(huì)得到正確結(jié)果,需要的朋友可以參考下2014-08-08Python單元測(cè)試及unittest框架用法實(shí)例解析
這篇文章主要介紹了Python單元測(cè)試及unittest框架用法實(shí)例解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07python使用str & repr轉(zhuǎn)換字符串
str() 是將 數(shù)值 轉(zhuǎn)成字符串 repr() 是將一個(gè)對(duì)象 轉(zhuǎn)成字符串 顯示 看明白了么:str()是將一個(gè)對(duì)象轉(zhuǎn)化成str對(duì)象 repr()是將一個(gè)對(duì)象可視化,使用字符串輸出,使編程人員看到其的內(nèi)部結(jié)構(gòu)2016-10-10Jupyter Notebook/VSCode導(dǎo)出PDF中文不顯示的解決
這篇文章主要介紹了Jupyter Notebook/VSCode導(dǎo)出PDF中文不顯示的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06pycharm+PyQt5+python最新開(kāi)發(fā)環(huán)境配置(踩坑)
這篇文章主要介紹了pycharm+PyQt5+python最新開(kāi)發(fā)環(huán)境配置(踩坑),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-02-02python中threading超線(xiàn)程用法實(shí)例分析
這篇文章主要介紹了python中threading超線(xiàn)程用法,實(shí)例分析了Python中threading模塊的相關(guān)使用技巧,需要的朋友可以參考下2015-05-05