python 模組之-re

來源:互聯網
上載者:User

標籤:iter   整數   尋找   分組   .com   pre   表達   call   one   

就其本質而言,Regex(或 RE)是一種小型的、高度專業化的程式設計語言,(在Python中)它內嵌在Python中,並通過 re 模組實現。Regex模式被編譯成一系列的位元組碼,然後由用 C 編寫的匹配引擎執行。

 

匹配案例:

import re ret=re.findall(‘a..in‘,‘helloalvin‘)print(ret)#[‘alvin‘]  ret=re.findall(‘^a...n‘,‘alvinhelloawwwn‘)print(ret)#[‘alvin‘]  ret=re.findall(‘a...n$‘,‘alvinhelloawwwn‘)print(ret)#[‘awwwn‘]  ret=re.findall(‘a...n$‘,‘alvinhelloawwwn‘)print(ret)#[‘awwwn‘]  ret=re.findall(‘abc*‘,‘abcccc‘)#貪婪匹配[0,+oo]  print(ret)#[‘abcccc‘] ret=re.findall(‘abc+‘,‘abccc‘)#[1,+oo]print(ret)#[‘abccc‘] ret=re.findall(‘abc?‘,‘abccc‘)#[0,1]print(ret)#[‘abc‘]  ret=re.findall(‘abc{1,4}‘,‘abccc‘)print(ret)#[‘abccc‘] 貪婪匹配
注意:前面的*,+,?等都是貪婪匹配,也就是儘可能匹配,後面加?號使其變成惰性匹配
ret=re.findall(‘abc*?‘,‘abcccccc‘)print(ret)#[‘ab‘]


元字元之字元集[]:

#--------------------------------------------字元集[]
ret = re.findall( ‘a[bc]d‘ , ‘acd‘ ) print (ret) #[‘acd‘]  ret = re.findall( ‘[a-z]‘ , ‘acd‘ ) print (ret) #[‘a‘, ‘c‘, ‘d‘]  ret = re.findall( ‘[.*+]‘ , ‘a.cd+‘ ) print (ret) #[‘.‘, ‘+‘]  #在字元集裡有功能的符號: - ^ \  ret = re.findall( ‘[1-9]‘ , ‘45dha3‘ ) print (ret) #[‘4‘, ‘5‘, ‘3‘]  ret = re.findall( ‘[^ab]‘ , ‘45bdha3‘ ) print (ret) #[‘4‘, ‘5‘, ‘d‘, ‘h‘, ‘3‘]  ret = re.findall( ‘[\d]‘ , ‘45bdha3‘ ) print (ret) #[‘4‘, ‘5‘, ‘3‘] 元字元之轉義符\

反斜線後邊跟元字元去除特殊功能,比如\.
反斜線後邊跟一般字元實現特殊功能,比如\d

\d  匹配任何十進位數;它相當於類 [0-9]。
\D 匹配任何非數字字元;它相當於類 [^0-9]。
\s  匹配任何空白字元;它相當於類 [ \t\n\r\f\v]。
\S 匹配任何非空白字元;它相當於類 [^ \t\n\r\f\v]。
\w 匹配任何字母數字字元;它相當於類 [a-zA-Z0-9_]。
\W 匹配任何非字母數字字元;它相當於類 [^a-zA-Z0-9_]
\b  匹配一個特殊字元邊界,比如空格 ,&,#等

 

ret=re.findall(‘I\b‘,‘I am LIST‘)

print (ret) #[] ret = re.findall(r ‘I\b‘ , ‘I am LIST‘ ) print (ret) #[‘I‘] 
#-----------------------------eg1:import reret=re.findall(‘c\l‘,‘abc\le‘)print(ret)#[]ret=re.findall(‘c\\l‘,‘abc\le‘)print(ret)#[]ret=re.findall(‘c\\\\l‘,‘abc\le‘)print(ret)#[‘c\\l‘]ret=re.findall(r‘c\\l‘,‘abc\le‘)print(ret)#[‘c\\l‘] #-----------------------------eg2:#之所以選擇\b是因為\b在ASCII表中是有意義的m = re.findall(‘\bblow‘, ‘blow‘)   #不加r print(m)#[]m = re.findall(r‘\bblow‘, ‘blow‘)  #加了rprint(m)#[‘blow‘]
元字元之分組()m = re.findall(r‘(ad)+‘, ‘add‘)print(m) ret=re.search(‘(?P<id>\d{2})/(?P<name>\w{3})‘,‘23/com‘)print(ret.group())#23/comprint(ret.group(‘id‘))#23
元字元之|ret=re.search(‘(ab)|\d‘,‘rabhdg8sd‘)print(ret.group())#ab

 

re模組下的常用方法import re#1re.findall(‘a‘,‘alvin yuan‘)    #返回所有滿足匹配條件的結果,放在列表裡#2re.search(‘a‘,‘alvin yuan‘).group()  #函數會在字串內尋找模式比對,只到找到第一個匹配然後返回一個包含匹配資訊的對象,該對象可以                                     # 通過調用group()方法得到匹配的字串,如果字串沒有匹配,則返回None。 #3re.match(‘a‘,‘abc‘).group()     #同search,不過盡在字串開始處進行匹配 #4ret=re.split(‘[ab]‘,‘abcd‘)     #先按‘a‘分割得到‘‘和‘bcd‘,在對‘‘和‘bcd‘分別按‘b‘分割print(ret)#[‘‘, ‘‘, ‘cd‘] #5ret=re.sub(‘\d‘,‘abc‘,‘alvin5yuan6‘,1)print(ret)#alvinabcyuan6ret=re.subn(‘\d‘,‘abc‘,‘alvin5yuan6‘)print(ret)#(‘alvinabcyuanabc‘, 2) #6obj=re.compile(‘\d{3}‘)ret=obj.search(‘abc123eeee‘)print(ret.group())#123

  

import reret=re.finditer(‘\d‘,‘ds3sy4784a‘)print(ret)        #<callable_iterator object at 0x10195f940> print(next(ret).group())print(next(ret).group())import re ret=re.findall(‘www.(baidu|oldboy).com‘,‘www.oldboy.com‘)print(ret)#[‘oldboy‘]     這是因為findall會優先把匹配結果組裡內容返回,如果想要匹配結果,取消許可權即可 ret=re.findall(‘www.(?:baidu|oldboy).com‘,‘www.oldboy.com‘)print(ret)#[‘www.oldboy.com‘]import reprint(re.findall("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>"))print(re.search("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>"))print(re.search(r"<(\w+)>\w+</\1>","<h1>hello</h1>"))#匹配出所有的整數import re#ret=re.findall(r"\d+{0}]","1-2*(60+(-40.35/5)-(-4*3))")ret=re.findall(r"-?\d+\.\d*|(-?\d+)","1-2*(60+(-40.35/5)-(-4*3))")ret.remove("")print(ret)

  

  

  

 

 

 

  

 

python 模組之-re

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.