1. 程式人生 > >python3 正則表達式(python3入門)

python3 正則表達式(python3入門)

模塊 dot ddc bbb 然而 all 出現的次數 變量 一行

  1 # 一、正則表達式:
  2 # \w    表示數字、字母、下劃線
  3 # \W     表示上面描述的內容取反
  4 #
  5 #
  6 # \s     表示任意空白字符(\t \n)
  7 # \S     表示上面描述的內容取反
  8 #
  9 # \d     [0-9]數字
 10 # \D     非數字 [^\d]
 11 #
 12 # ^    表示從開頭開始匹配
 13 # $    表示從結尾開始匹配
 14 #
 15 # .    代表任意非一個字符(除了\n以外)
 16 # 若要讓. 匹配所有包括換行符號,則表達式為中的參數需加上re.DOTALL
17 # print(re.findall(‘a.c‘,‘abc a c a1c aaaaa a\nc a\tc‘,re.DOTALL)) 18 19 20 # [] 表示匹配一個字符,這個字符必須是指定範圍內的 21 # [0-9]表示0-9任意一個數字,[a-zA-Z]表示大小寫a-z中任意一個字母 22 # print(re.findall(‘a[0-9]c‘,‘abc a c a1c aaaaa a\nc a\tc‘)) 23 # print(re.findall(‘a[a-z]‘,‘abc a c a1c aaaaa a\nc a\tc‘)) 24 # print(re.findall(‘a[+=#!-]c‘,‘a#c a!c a+c a-c aa abc a1c a\nc‘))
25 26 27 28 #二、重復匹配: 29 #? 表示?左邊的一個字符出現0次或者1次(通俗點來說,如果?左邊的字符有,就取,如果沒有則不取) 30 # print(re.findall(‘ab?‘,‘ab abb abbbb abbbbbbb a1b a+b‘)) #[‘ab‘, ‘ab‘, ‘ab‘, ‘ab‘, ‘a‘, ‘a‘] 31 32 #* 表示左邊的一個字符出現0次或者無窮次 33 # print(re.findall(‘ab*‘,‘ab abb abbbb abbbbbbb a1b a+b‘)) #[‘ab‘, ‘abb‘, ‘abbbb‘, ‘abbbbbbb‘, ‘a‘, ‘a‘]
34 35 #.* 表示任意0個或者無窮個非換行字符(貪婪匹配,找到離他最遠的字符) 36 # print(re.findall(‘a.*c‘,‘ac a12312131dsssssdddss466668948949239kddddccdsdfacdc 123 a\nc‘)) 37 #[‘ac a12312131dsssssdddss466668948949239kddddccdsdfacdc‘] 38 39 #.*? 非貪婪匹配(固定搭配,?在這裏不是之前?的意思了) 40 # print(re.findall(‘a.*?c‘,‘ac a1c2312131dsssssdddss466668948949239kddddccdsdfacdc 123 a\nc‘)) #[‘ac‘, ‘a1c‘, ‘ac‘] 41 42 43 #| 表示或者,滿足其一都可以 44 # print(re.findall(‘company|companies‘,‘Too many companies have gone bankrupt, and the next one is my company‘)) 45 # print(re.findall(‘compan(?:y|ies)‘,‘Too many companies have gone bankrupt, and the next one is my company‘)) 46 #兩種方法都可以 47 48 49 #() 指定表達式,但是只保留()內所指定的內容,若想要取()外的內容,則可以寫成(?:過濾內容) ?:為固定搭配 50 #可以認為()左邊所定義的內容為匹配的條件。 51 # print(re.findall(‘compan(y|ies)‘,‘Too many companies have gone bankrupt, and the next one is my company‘)) 52 # print(re.findall(‘compan(?:y|ies)‘,‘Too many companies have gone bankrupt, and the next one is my company‘)) 53 #使用分組+非貪婪 表達式獲取字符串中的url地址 54 # print(re.findall(‘href="(.*?)"‘,‘<a href="https://www.baidu.com/1.mp4"><p>"哈哈哈"</p><a href="https://www.baidu.com/2.mp4">‘)) 55 56 57 #+ 代表左邊的那個字符出現1次或者無窮次(+ 和 *進行對比) 58 # print(re.findall(‘ab*‘,‘a ab abb abbb abbbbbbb a1b a+b‘)) 59 # print(re.findall(‘ab+‘,‘a ab abb abbb abbbbbbb a1b a+b‘)) 60 61 62 #\ 轉義符,\後面的字符,取消特殊字符的意義 63 #python解釋器使用的是C解釋器的功能,所以在python語法解析後,會剝離掉一個\ ,然後再給c解釋器的時候,就變成另外一層意思了 64 65 # print(re.findall(‘a\.c‘,‘a.c a1c aac aAc‘)) 66 # print(re.findall(‘a\\.‘,‘a\c ac ab2 a. ac‘)) #先經過python語法解析後,正則條件會變成‘a\c‘,然而再給到c解釋器的時候,就認為 67 # #是在匹配‘a\c‘,此時a+不轉譯c 68 # 69 # print(re.findall(r‘a\\\.‘,‘a\c ac ab2 a\. ac‘)) #效果和下面一致 70 # print(re.findall(‘a\\\\\\.‘,‘a\c ac ab2 a\. ac‘)) #效果和上面一致 71 # 72 # 73 # print(re.findall(‘a\\\\c‘,‘a\c ac ab2 ac‘)) 74 # print(re.findall(r‘a\\c‘,‘a\c ac ab2 ac‘)) 75 76 77 78 #小練習 79 # print(re.findall(‘\D?(\-?\d+\.?\d*)‘,"1-12*(60+(-40.9834898/5)-(-4*3)")) #取之字符串中所有的整數 80 81 #{} 自定義某個字符出現的次數 #{0,2} => 表示出現的次數為0~2次,{}左邊的字符如果有,則至多取兩次,如果沒有,則不取 82 # print(re.findall(‘ab{0,2}‘,‘a ab abb abbbb‘)) 83 84 85 86 87 #re模塊其他的使用方法 88 x = myleon leon is DSB 89 90 #re.search 從頭開始查找,若找到就返回查找到的對象,可以通過.group()提取該值 91 # print(re.search(‘leon‘,x)) #從左往右查找,返回第一個查找到的對象 92 # print(re.search(‘leon‘,x).group()) #根據查找到的對象返回一個最終的值 93 # print(re.search(‘^leon‘,x)) #查找以leon開頭的結果,效果等同re.match 94 95 #re.match() 只對開頭開始匹配 96 # print(re.match(‘leon‘,x)) #查找以alex開頭的值,返回一個對象,若被查找的字符串開頭不是這個查詢條件,則返回None 97 # print(re.match(‘leon‘,x).group()) #返回一個查找的結果值,None是沒有辦法返回值的 98 99 #re.split() 可以指定正則來進行切分 100 # info = ‘a*-=b->:c‘ #無法使用‘‘.split()來進行分割,每次符合正則條件的時候將左邊的放入列表中 101 # print(re.split(‘[*=>-]‘,info)) #[‘a‘, ‘‘, ‘‘, ‘b‘, ‘‘, ‘c‘] 102 # res = [i for i in re.split(‘[*-=>]‘,info) if len(i) != 0] #通過三元表達式+re.split 103 # print(res) 104 105 #re.sub() #類似‘‘.replace的功能 106 # msg1 = ‘alex myalex alex is DSB‘ 107 # print(re.sub(‘^alex‘,‘ALEX‘,msg1)) #ALEX myalex alex is DSB 108 # print(msg1.replace(‘alex‘,‘ALEX‘)) #ALEX myALEX ALEX is DSB 109 110 # msg2=‘alex my name is alex Alex AlEx aLeX is hahahah‘ #將不論大小寫的alex 都替換成DSB 111 # print(re.sub(‘alex‘,‘DSB‘,msg2,flags=re.I)) #flag=I 意思為忽略大小寫 112 113 114 # msg3=""" 115 # a1231231231231egon 116 # 123123sadfsadf2143egon 117 # sdafljsalfdjerwqregon 118 # """ 119 # print(re.findall(‘egon$‘,msg3,flags=re.M)) #flag=re.M 意思為每一行都取 120 121 122 123 #了解 re.sub()+分組+序號 進行重新排序 124 # msg4="Name---@#@#--is==++*=Pig" 125 # print(re.sub(‘([a-zA-Z]+)([^a-zA-Z]+)([a-zA-Z]+)([^a-zA-Z]+)([a-zA-Z]+)‘,r‘\5\2\3\4\1‘,msg4)) 126 127 #re.compile() 將正則表達式存為變量,然後可以重復調用 128 # patten = re.compile(‘href="(.*?)"‘) 129 # print(patten.findall(‘<a href="https://www.baidu.com/1.mp4"><p>"哈哈哈"</p><a href="https://www.baidu.com/2.mp4">‘)) 130 # print(patten.findall(‘<a href="https://www.kaixin.com/png1"><p>"哈哈哈"</p><a href="https://www.kaixin001.com/png2">‘))

python3 正則表達式(python3入門)