python學習day12

來源:互聯網
上載者:User

標籤:pre   one   nbsp   gpo   pytho   python   表示範圍   ref   class   

Regex的學習

 

#codeing:UTF-8#__author__:Duke#date:2018/3/17/017 23:29import re#方法一  findall  全找ret = re.findall(‘w\w{2}‘,‘hello world‘)print(ret)# 元字元  11個# .  萬用字元ret = re.findall(‘w..‘,‘hello world‘)  # w..  點點任意指代,一個代指一個字元print(ret)ret = re.findall(‘w.l‘,‘hello w\rld‘)  # w..  點點任意指代,一個代指一個字元print(ret)   #  不能指代“\"字元# ^   尖角符ret = re.findall("^h...o","hjasdfhello")   #只從開始匹配print(ret)#  $  dollor  符號ret = re.findall("d..e$","hjasdukefhflle")   #只從末尾匹配print(ret)#  * 重複匹配   [0  無窮)個ret = re.findall("du*","hjasdukefhfllfdukeeerffrefwegduuuuekw")   #只從末尾匹配print(ret)#  + 重複匹配   [1  無窮)個ret = re.findall("du+","hjasdukefhfllfdkeeerffrefwegduuuuekw")print(ret)   #至少一個u# #  + 重複匹配   [1  無窮)個ret = re.findall("du+","hjasdukefhfllfdkeeerffrefwegduuuuekw")print(ret)   #至少一個u#  ?   0或則1個 dret = re.findall("d?u","hjasdukefhfllfdkeeerffrefwegduuuuekw")print(ret)# { }  在區間內均可ret = re.findall("du{1,2}","hjasdukefhfllfdkeeerffrefwegduuuuekw")print(ret)   #貪難匹配,按最多的匹配# 結論:* =={0,+無窮}   + == {1,+無窮}    ?== {0,1}# []   字元集ret = re.findall(‘a[c,d,e,s,]x‘,‘asx‘)   #是 or 意思print(ret)ret = re.findall(‘[a-z]‘,‘asx‘)   # - 表示範圍print(ret)#     注意 [] 字元集  :取消元字元的作用、  (  \  ^  - )這三個除外ret = re.findall(‘[w,,]‘,‘awdx*,‘)   #  *變為一般字元print(ret)ret = re.findall(‘[^a]‘,‘awdx*,‘)   #  ^  取補集  即為除  a 的元素print(ret)ret = re.findall(‘[^a,w]‘,‘awdx*,‘)   #  []  中的全部元素非  包括,print(ret)  #[‘d‘, ‘x‘, ‘*‘]#  \  斜杠#斜杠後的元字元 去除特殊功能#斜杠後的一些一般字元實現特殊功能#  \d  表示數字  [0,9]print(re.findall(‘\d{11}‘,‘hqfuhi8712648721485‘))   #匹配11個數字# [‘87126487214‘]#  \D 表示[^0=9]print(re.findall(‘\D{5}‘,‘hqfuhi8712648721485‘))   #匹配5個字元# # [‘hqfuh‘]# #  \s 表示任意Null 字元]print(re.findall(‘\s‘,‘hqfu\thi87 1264\r8721485‘))# # [‘\t‘, ‘ ‘, ‘\r‘]# #  \S 表示任意非Null 字元]print(re.findall(‘\S‘,‘hqfu\thi87 1264\r8721485‘))# [‘h‘, ‘q‘, ‘f‘, ‘u‘, ‘h‘, ‘i‘, ‘8‘, ‘7‘, ‘1‘, ‘2‘, ‘6‘, ‘4‘, ‘8‘, ‘7‘, ‘2‘,#  \w 表示任一數字  字母  字元]#  \W 表示任意非數字  字母  字元]#  \b 表示特殊字元的邊界print(re.findall(r‘\bi‘,‘hello i an duke‘))  #\+ret = re.search(‘a+‘,‘aaawdx*,‘)   #print(ret.group())  #aaaret = re.search(‘a\+‘,‘aaawdx*,‘)   #print(ret)  #None# 兩種方法解決標紅ret = re.findall(r‘\\‘,‘aaawdxD:\c‘)   #print(ret)  #[‘\\‘]ret = re.findall(‘\\\\‘,‘aaawdxD:\c‘)   #print(ret)  #[‘\\‘]#   用結果理解ret = re.search(r‘\bduke‘,‘duke‘)   #  r表示原生字串print(ret.group())  #dukeret = re.search(‘\bduke‘,‘duke‘)   #  r表示原生字串print(ret)  #None#  ()做分組   |print(re.search(‘(as)+‘,‘sdjkasf‘).group())  #asprint(re.search(‘(as+)‘,‘sdjkasf‘).group())  #asprint(re.search(‘(as)|3‘,‘sdj3kas3f3‘).group())  #整體或print(re.findall(‘(as)|3‘,‘sdj3kas3f3‘) ) #[‘‘, ‘as‘, ‘‘, ‘‘]# #用於檔案路徑ret = re.search(‘(?P<id>\d{3})/(?P<name>\w{3})‘,‘dghwwweeejyaf123/ooo‘)print(ret.group())print(ret.group(‘id‘))print(ret.group(‘name‘))# 二:re  的方法#findall()  :返回全部的結果,以list的形勢# re.search()  :返回一個對象 ,對象通過 group方法返回結果# re.match()   只在字串開始開始匹配  ,返回的也是一個對象  group# re.split()  :print(re.split(‘[k,s]‘,‘dukehsijik‘))   #這個很重要print(re.split(‘[k,s]‘,‘dskehsijik‘))   #注意這個的分法#sub()  替換print(re.sub(‘d..e‘,‘haha‘,‘jiewjfrewdukeshjs‘))principle= re.compile(‘d..e‘)  #編譯規則ret = principle.findall(‘iewjfrewdukeshjs‘)print(ret)   #[‘duke‘]

 

python學習day12

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.