re模組與正則表示式
首先要先繼承re模組: import re
re.findall() 方法 # 返回值為列表
\w 表示一個字元,為數字,字母,下滑線之一, \W匹配任意非數字,字母,下劃線
print(re.findall('\w3\w','abc3dafg375983_')) # 輸出結果為: ['c3d', 'g37', '83_']
print(re.findall('2\W','abc2_ 2 3| 2|')) # 輸出結果為: ['2 ', '2|']
\s 表示匹配 任意空白字元(\t \n \r \f 分別表示:水平製表符,移動到下一個tab的位置; 換行符; 回車; 換頁) \S匹配任意 非空字元
print(re.findall('2\s','abc2\f_ 2 3| 2\t')) # 輸出結果為 ['2\x0c', '2 ', '2\t'];
若將程式碼改為: print(re.findall('2\s','abc2\\f_ 2 3| 2\\t')) ,則 輸出結果為: ['2 ']
\d 匹配任意數字(0-9), \D匹配任意非數字
print(re.findall('\d','abc3dafg375983_')) #輸出結果為: ['3', '3', '7', '5', '9', '8', '3']
print(re.findall('\D','abc3dafg375983_')) #輸出結果為: ['a', 'b', 'c', 'd', 'a', 'f', 'g', '_']
\Ax 匹配字串開始(x表示需要找的字串),如字串是以要找的字串開頭,則返回這個字串,若不是則結束匹配
print(re.findall('\Aa','abc3dafg375983_')) #輸出結果為: ['a']
x\Z 匹配字串結束(x表示需要找的字串)
print(re.findall('3\Z','abc3dafg375983_')) #輸出結果為: [ ] 表示空列表,表示字串不是以3結尾
^ 字串開頭匹配(判段開頭是否為要找的字串,並返回值,若不是,則結束尋找)
$ 從末尾開始匹配(判段結尾是否為要找的字串,並返回值,若不是,則結束尋找)
print(re.findall('^c3','c3abc3dafg37c3983_')) 輸出結果為: ['c3']
print(re.findall('c3$','c3abc3dafg37c398c3_')) 輸出結果為: [ ]
. :匹配除換行符之外的任意字元, 加上flag:re.DOTALL 即可匹配換行符
print(re.findall('a.c','abc a c a|ca#ckfaabbc')) 輸出結果為:['abc', 'a c', 'a|c', 'a#c']
[...] :表示匹配一組字元中的一個字元
print(re.findall('[b.]','abc a c a|ca#ckfa.abbc')) 輸出結果為: ['b', '.', 'b', 'b']
print(re.findall('a[a-zA-Z]c','aBcfagchka|c',re.DOTALL)) 輸出結果為: ['aBc', 'agc']
重複匹配:
? :表示在?左邊的字元出現0次或1次
print(re.findall('ab?','a ab abb abc acf b ab a21b')) # 在此情況下表示 , b出現一次或0次的字串,結果為: ['a', 'ab', 'ab', 'ab', 'a', 'ab', 'a']
* :0次到無窮多次
print(re.findall('ab*','aababbaaabbb')) #輸出結果為: ['a', 'ab', 'abb', 'a', 'a', 'abbb']
+ :1次或無窮多次
print(re.findall('ab+','a ab abb bb abbb aaabbb')) # 輸出結果為: ['ab', 'abb', 'abbb', 'abbb']
.* :貪婪匹配,儘可能多的吞噬字元
print(re.findall('a.*c','aasfakfipgld csagsdpp cas')) # 輸出結果為: ['aasfakfipgld csagsdpp c']
.*? : 非貪婪匹配,儘可能少的吞噬字元
print(re.findall('a.*?c','aasfakfipgld csagsdpp cas')) # 輸出結果為: ['aasfakfipgldc', 'agsdpp c']
| :表示或者
print(re.findall('yang|huang','yang is a boy huang is a girl')) #輸出結果為: ['yang', 'huang']
() : 表示分組,預設情況下只保留括號內的資料, 在括號內加入?:則會保留完整的 (此處有誤,正確答案會在之後更新)
print(re.findall('email.:(?:.*?) ','email1:[email protected] email2:[email protected] eamil3:[email protected]'))
輸出結果為: ['[email protected]', '[email protected]']
print(re.findall('email.:(.*?)','email1:[email protected] email2:[email protected] eamil3:[email protected]'))
輸出結果為:
re.I # 忽略大小寫
print(re.findall('alex','aLex is Alex is alExandaleX',re.I)) # 輸出結果為:['aLex', 'Alex', 'alEx', 'aleX']
re.M #忽略換行符
msg="""
ahfquqfqioiyang
sfahsifpofayang
fdi352526yang
# """
print(re.findall('yang$',msg,re.M)) # 輸出結果為: ['yang','yang','yang']
re.search()方法 找到一個即停止
print(re.search('alex','alex is alex is alex')) # 輸出結果為: <_sre.SRE_Match object; span=(0, 4), match='alex'>
re.match()方法 找開頭,沒有找到則結束
print(re.match('alex','alex is alex is alex')) # 輸出結果為: <_sre.SRE_Match object; span=(0, 4), match='alex'>
從上述程式碼可以看出search方法與match方法返回結果比較相似
小練習:找到以下字串中所有的數字(正數,負數,小數):
msg="1-2*(60+(-40.35/5)-(-4*3))"
程式碼為:
print(re.findall('\D?(-?\d+\.?\d*)',msg))
結果為:
['1', '2', '60', '-40.35', '5', '-4', '3']