色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

Python正則re模塊使用步驟及原理解析

瀏覽:5日期:2022-07-13 16:07:54

python中使用正則表達式的步驟:

1.導入re模塊:import re

2.初始化一個Regex對象:re.compile()

3.剛剛創(chuàng)建的Regex對象調(diào)用search方法進行匹配,返回要給March對象

4.剛剛的March對象調(diào)用group方法,展示匹配到的字符串

下面例子的知識點:

對正則表達式分組用:(),正則里的分組計數(shù)從1開始,不是從0,切記~~

group(數(shù)字):去對應的分組的值 groups():返回所有分組的元組形式

d表示一個數(shù)字

regex_obj = re.compile(r’(ddd)-(ddd)-(dddd)’)match_obj = regex_obj.search(’我司電話: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í)行結果:

0354111234035-411-1234(’035’, ’411’, ’1234’)

補充知識點:w表示一個單詞,s表示一個空格

regex_obj = re.compile(r’(dwd)-(ddd)-(dddd)’)match_obj = regex_obj.search(’我司電話:0a5-411-1234’)result = match_obj.group(1)print(result)regex_obj = re.compile(r’(dwd)-(ddd)-(dddd)’)match_obj = regex_obj.search(’我司電話:0哈5-411-1234’)result = match_obj.group(1)print(result)regex_obj = re.compile(r’(dsd)-(ddd)-(dddd)’)match_obj = regex_obj.search(’我司電話:0 5-411-1234’)result = match_obj.group(1)print(result)

執(zhí)行結果:

0a50哈50 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í)行結果:

successfully200ok

注意:如果search返回的March對象只有一個結果值的話,不能用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í)行結果:

()successfully

? :可選匹配項

+ :1次 或 n次 匹配

* :*前面的字符或者字符串匹配 0次、n次

注意:*前面必須要有內(nèi)容

 regex_obj = re.compile(r’(haha)*,welcome to vom_admin system’) 指haha這個字符串匹配0次或者多次

 regex_obj = re.compile(r’(ha*),welcome to vom_admin system’) 指ha這個字符串匹配0次或者多次

. : 通配符,匹配任意一個字符

所以常常用的組合是:.*

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í)行結果:

Peter

{} : 匹配特定的次數(shù)

里面只寫一個數(shù)字:匹配等于數(shù)字的次數(shù)

里面寫{3,5}這樣兩個數(shù)字的,匹配3次 或 4次 或 5次,按貪心匹配法,能滿足5次的就輸出5次的,沒有5次就4次,4次也沒有才是3次

regex_obj = re.compile(r’((ha){3}),this is very funny’)match_obj1 = regex_obj.search(’hahahaha,this is very funny’)print('{3}結果',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}結果',match_obj1.group(1))

執(zhí)行結果:

{3}結果 hahaha{3,5}結果 hahahaha

findall():返回所有匹配到的字串的列表

regex_obj = re.compile(r’ddd’)match_obj = regex_obj.findall(’我是101班的,小李是103班的’)print(match_obj)regex_obj = re.compile(r’(ddd)-(ddd)-(dddd)’)match_obj = regex_obj.findall(’我家電話是123-123-1234,我公司電話是890-890-7890’)print(match_obj)

打印結果:

[’101’, ’103’][(’123’, ’123’, ’1234’), (’890’, ’890’, ’7890’)]

[]:創(chuàng)建自己的字符集:

[abc]:包括[]內(nèi)的字符

[^abc]:不包括[]內(nèi)的所有字符

也可以使用:[a-zA-Z0-9]這樣簡寫

regex_obj = re.compile(r’[!@#$%^&*()]’)name = input('請輸入昵稱,不含特殊字符:')match_obj = regex_obj.search(name)if match_obj: print('昵稱輸入不合法,包含了特殊字符:', match_obj.group())else: print('昵稱有效')

執(zhí)行結果:

請輸入昵稱,不含特殊字符:*h昵稱輸入不合法,包含了特殊字符: *

 ^:開頭

 $:結尾 

regex_obj = re.compile(r’(^[A-Z])(.*)’)name = input('請輸入昵稱,開頭必須大寫字母:')match_obj = regex_obj.search(name)print(match_obj.group())

執(zhí)行結果:

請輸入昵稱,開頭必須大寫字母:A1234A1234

sub():第一個參數(shù)為要替換成的,第二個參數(shù)傳被替換的,返回替換成功后的字符串

regex_obj = re.compile(r’[!@#$%^&*()]’)match_obj = regex_obj.sub(’嘿’,’haha,$%^,hahah’)print(match_obj)

執(zhí)行結果:

haha,嘿嘿嘿,hahah

補充一下正則表達式的表,正則太復雜了,要常看常用才能熟練

Python正則re模塊使用步驟及原理解析

Python正則re模塊使用步驟及原理解析

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 免费人成综合在线视频 | 曰本黄页 | 精品久久久久久久久中文字幕 | 国产第一夜 | 亚洲日本久久一区二区va | 久久91在线 | 久久精品国产在爱久久 | 最近韩国日本免费免费版 | 最新福利片v国产片 | 欧美日韩第二页 | 亚洲高清自拍 | 交videos人妖| 一级女性全黄久久生活片 | 久久久一本| 成年人视频在线免费播放 | 九九亚洲 | 日韩一品在线播放视频一品免费 | 久久成人性色生活片 | 欧美另类交视频 | 99久久国产免费中文无字幕 | 三级网站| 国产精品视频免费观看调教网 | 久久国产精品无码网站 | 一级特黄aa大片欧美 | 国产精品偷伦费观看 | 国产精品热久久毛片 | 毛片网站在线看 | 久久久久久免费一区二区三区 | 日本黄页网站免费大全 | 亚洲在线久久 | 亚洲最大网址 | 亚洲精品午夜久久久伊人 | 精品国产一区二区在线观看 | 国产精品亚洲成在人线 | a级成人毛片免费视频高清 a级高清观看视频在线看 | 男女视频在线观看免费 | 欧美日韩在线永久免费播放 | 三级做人爱c视频18三级 | 亚洲国产一区二区a毛片日本 | 欧美一级毛片一级 | a级黄色毛片免费播放视频 a级精品九九九大片免费看 |