re.findall 匹配到正則表達式的字符,匹配到的每個字符存入一個列表,返回一個匹配到的所有字符列表
一. 匹配單個字符
import re # \w 匹配所有字母、數字、下划線 re.findall('\w','abcd_123 *-') # 結果為:['a', 'b', 'c', 'd', '_', '1', '2', '3'] # \s 匹配所有不可見字符 # 不可見字符有:\n \t 空格 re.findall('\s','abcd \n\tdf21 ') # 結果為:[' ', '\n', '\t', ' ', ' '] # \d 匹配所有數字 re.findall('\d','a1bc3 4d \n\tdf21 ') # 結果為:['1', '3', '4', '2', '1'] # . 匹配\n換行符以外的所有可見和不可見字符 re.findall('.', 'c3 d\n\td1') # 結果為:['c', '3', ' ', 'd', '\t', 'd', '1']
二. 匹配多個字符
#匹配多個字符:* + ? import re # * 匹配0或n次 re.findall('\d*', '1a23') # 結果為:['1', '', '23', ''] # + 匹配1或n次 re.findall('\d+', '1a23') # 結果為:['1', '23'] #前面2個是叫貪婪匹配 # ? 匹配0或1次 re.findall('\d?', '1a3') # 結果為:['1', '', '3', ''] # 這個叫非貪婪匹配
三. 匹配指定范圍字符
# {m,n} 匹配最少m次,最多n次 # {m} 只匹配m次的 # {,n} 匹配最大n次,最小次數不限 # {m,} 匹配最小m次,最大次數不限 re.findall('\d{2,4}', '1a33g134nn12345') # 結果為:['33', '134', '1234'] re.findall('a{3}', '1aaa3*G+1aaaan#b') # 結果為:['aaa', 'aaa'] # | 匹配 或 語法的字符 re.findall('1|23|4', '1a33g134nn12345') # 結果為:['1', '1', '4', '1', '23', '4'] # [ ] 匹配括號內的字符(只匹配單個) re.findall('[123]', '1a33g134nn12345') # 結果為:['1', '3', '3', '1', '3', '1', '2', '3'] re.findall('[0-9a-zA-Z]', '1a3*G+1n#') # 匹配0到9或者a到z或者A到Z單個字符 # 結果為:['1', 'a', '3', 'G', '1', 'n'] # ^x 行首匹配 # x$ 行尾匹配 re.findall('^1', '1a3*G+1n#b') re.findall('b$', '1a3*G+1n#b') # 結果為:['1'] # 結果為:['b']
四. 一些比較不常見的匹配
\b 單詞邊界,重點理解在於‘邊界’二字,‘單詞’代表匹配的字符串
划定邊界基本原則:比如表達式為 '\b單詞\b' 的意思就是匹配字符串為‘單詞’,單詞兩邊需要存在滿足條件的邊界才能匹配成功。匹配失敗結果肯定是空列表,匹配成功結果就是匹配的單詞字符串列表。
\b在哪邊就限制哪邊的匹配。
匹配成功的邊界滿足條件跟匹配單詞字符串有關聯,雙方是同一類型則失敗,不同類型則成功
# 正則表達式中的\b可以用匹配目標單詞字符串,加設限制匹配邊界 # 單詞的 前邊界:'\b單詞' # 后邊界:'單詞\b' # 前和后邊界:'\b單詞\b' # 在\b的使用中有這樣幾點是需要格外注意的。 # 0. \b本身的含義就有轉義字符串的作用,要用字符邊界就得將其原始含義去掉,所以需要加r或者\\b(兩個不能一起用) # 1. 單詞字符串能否匹配成功是有規律的: # 如果是a-z,A-Z,0-9,則匹配邊界為非字母數字才能匹配成功。 # 如果是非字母數字,則匹配邊界為a-z,A-Z,0-9才能匹配成功。 # 如果是兩個混在一起,判斷標准為那個挨着\b最近的字符作為判斷標准,如果是字母數字,則匹配邊界為非字母數字才成功,反之亦然。 import re s='aa abcperty/-aa ' res1=re.findall(r'\babc',s) # 結果:['abc'] 單詞為字母,左匹配邊界為非字母數字,匹配成功 res2=re.findall(r'abc\b',s) # 結果:[] 單詞為字母,右匹配邊界為字母,匹配失敗 print(res1,res2) s='aa@# perty/-aa ' res3=re.findall(r'\b@#',s) # 結果:['@#'] 單詞為非字母數字,左匹配邊界為字母,匹配成功 res4=re.findall(r'@#\b',s) # 結果:[] 單詞為非字母數字,右匹配邊界為空格(非字母數字),匹配失敗 print(res3,res4) s='aa@a perty/-aa ' res5=re.findall(r'\b@a',s) # 結果:['@a'] 單詞為混合,@挨着\b,單詞以非字母數字為標准,左匹配邊界為字母,匹配成功 res6=re.findall(r'@a\b',s) # 結果:['@a'] 單詞為混合,a挨着\b,單詞以字母數字為標准,右匹配邊界為空格,匹配成功 print(res5,res6) # 2.字符串的首尾邊界默認定義為 非字母數字,所以如果匹配的單詞字符串在首尾,則匹配字母數字則會成功,匹配非字母數字失敗。 s= 'abcd@' res7 = re.findall(r'\ba',s) # 結果:['a'] 單詞為字母,左匹配邊界為首,默認定義為非字母數字,匹配成功 res8 = re.findall(r'@\b',s) # 結果:[] 單詞為非字母數字,右匹配邊界為尾,默認定義為非字母數字,匹配失敗 print(res7,res8)
五. 分組
用小括號表示,分組后的正則表達式優先匹配並返回括號內的值
六. re模塊常用方法
re.search
查找,匹配成功返回第一匹配的字符串,結果為封裝的對象(注意search返回的是個對象,.group() 后得到的是字符串不是列表),失敗返回None
import re s = 'welcome world hello python' res = re.search('hello', s) print(res) print(res.group()) # 結果: <_sre.SRE_Match object; span=(14, 19), match='hello'> hello
re.match
匹配,在行首匹配字符串,方法同search,區別在於,match在行首匹配,search是從前到后全部查找匹配。
s = 'hello welcome world hello python' res=re.match('hello', s) print(res) print(res.group()) res1=re.match('welcome',s) print(res1) # 結果: <_sre.SRE_Match object; span=(0, 5), match='hello'> hello None
re.split
用匹配成功的字符串作為切割符,進行原字符串的切割,返回一個切割后的列表。匹配失敗,直接返回原字符串,不做任何切割處理
匹配成功完成切割 s1 = 'hello welchhome world hello python' res=re.split('h.+?o',s1) print(res) # 結果: ['', ' welc', 'me world ', ' python'] 匹配不成功,返回原字符串。不做任何切割處理 s1 = 'hello welchhome world hello python' res0=re.findall('json',s1) print(res0) res=re.split('json',s1) print(res) # 結果: [] ['hello welchhome world hello python']
re.compile
將正則表達式封裝成對象,用於重復使用該表達式。使用方法看例子:
s1 = 'hello welchhome world hello python' pat=re.compile('h.+?o') res2=pat.findall(s1) print(res2) # 結果: ['hello', 'hho', 'hello']
re.sub
替換,重組(group(1)、group(2)...group(n)用\1 \2 ...\n來替換,進行字符串的重新組合(前面要加r防止其轉義))
s='<我的博客地址https://www.cnblogs.com/oldboy2019/,歡迎大家來訪問、交流,歡迎大家一起探討python!ohye!>' # pat=re.compile(r'<(.+)http://(www.+/),.+>') pat=re.compile(r'<(.+)http.+(www.+/),(.+?)([a-z]+)!(.+)!>') print(pat.findall(s)) res=pat.sub(r'\2\n這是\1,\4,\5\3',s) print(res) # 結果: [('我的博客地址', 'www.cnblogs.com/oldboy2019/', '歡迎大家來訪問、交流,歡迎大家一起探討', 'python', 'ohye')] www.cnblogs.com/oldboy2019/ 這是我的博客地址,python,ohye歡迎大家來訪問、交流,歡迎大家一起探討
#注意,這里第二個打印我在前面加了個原義字符r,但是里面\n還是轉義成了換行,很奇怪,這個地方歡迎一起交流,為何會出現這種情況呢?
