python findall函数_Ramp;Python Data Science系列:数据处理(11)Python正则表达式re模块(三)...
前言
使用正則表達(dá)式進(jìn)行匹配,可以直接調(diào)用模塊級(jí)函數(shù),如match()、search()、findall()等,函數(shù)第一個(gè)參數(shù)是匹配的正則表達(dá)式,第二個(gè)參數(shù)則為要匹配的字符串。也可以使用re.compile()先將正則表達(dá)式編譯成RegexObject對(duì)象,然后再調(diào)用RegexObject對(duì)象的方法,參數(shù)為要匹配的字符串。例如:
re.search(r'flash', 'Flash_WorkingNotes', re.I).group()等價(jià)于
p = re.compile(r'flash', re.I) p.search('Flash_WorkingNotes').group()如果匹配的正則表達(dá)式只用一次,模塊級(jí)函數(shù)使用起來很方便;若項(xiàng)目中包含多個(gè)正則表達(dá)式或者一個(gè)正則表達(dá)式被多次使用,編譯成RegexObject對(duì)象更方便一些。以下內(nèi)容,先編譯成正則表達(dá)式對(duì)象,然后再調(diào)用這些對(duì)象的方法。
5.2.3 RegexObject的方法和MatchObject的方法
re模塊提供了一個(gè)正則表達(dá)式引擎接口,可以將正則表達(dá)式編譯成對(duì)象并用它們進(jìn)行匹配。使用re.compile()將正則表達(dá)式編譯成RegexObject對(duì)象,有對(duì)象就有方法可以調(diào)用,RegexObject對(duì)象常用方法有match()、search()、findall()、finditer()、split()、sub()以及subn()。
match()和search()匹配成功的話返回一個(gè)MatchObject實(shí)例,findall()、split()、sub()以及subn()返回一個(gè)列表,finditer()返回一個(gè)迭代器。
- match()函數(shù)
match()函數(shù)檢查RE是否在字符串開始處匹配,match()函數(shù)只返回一次成功的匹配,從0開始,如果不是從0匹配成功,返回None。如果匹配成功,返回一個(gè)MatchObject 對(duì)象,可以通過group方法獲取匹配成功的整個(gè)字符串。
1 匹配成功
p = re.compile('Flash', re.I) p.match('flash workingnotes')匹配成功的話,返回一個(gè)MatchObject對(duì)象,可以調(diào)用MatchObject的方法。
2 使用group()函數(shù)返回匹配成功的整個(gè)字符串
p = re.compile('Flash', re.I) p.match('flash workingnotes').group()3 使用start()返回匹配開始的位置
p = re.compile('Flash', re.I) p.match('flash workingnotes').start()4 使用end()返回匹配結(jié)束的位置
p = re.compile('Flash', re.I) p.match('flash workingnotes').end()5 使用span()返回一個(gè)元組包含匹配(開始,結(jié)束)的位置
p = re.compile('Flash', re.I) p.match('flash workingnotes').span()6 匹配失敗
p = re.compile('Flash', re.I) print(p.match('workingnotes flash'))- search()函數(shù)
search()函數(shù)檢查整個(gè)字符串,匹配成功,返回一個(gè)匹配對(duì)象MatchObject,沒有匹配成功返回None
1 匹配成功
p = re.compile('Flash', re.I) print(p.search('workingnotes flash'))2 使用group()函數(shù)返回匹配成功的整個(gè)字符串
p = re.compile('Flash', re.I) print(p.search('workingnotes flash').group())3 使用start()返回匹配開始的位置
p = re.compile('Flash', re.I) print(p.search('workingnotes flash').start())4 使用end()返回匹配結(jié)束的位置
p = re.compile('Flash', re.I) print(p.search('workingnotes flash').end())5 使用span()返回一個(gè)元組包含匹配(開始,結(jié)束)的位置
p = re.compile('Flash', re.I) print(p.search('workingnotes flash').span())6 匹配失敗
p = re.compile('Flash', re.I) print(p.search('workingnotes fash'))- findall()函數(shù)
findall()函數(shù)找到匹配成功的所有子串,并把它們作為一個(gè)列表返回,若沒有匹配成功,返回空列表
1 匹配成功
p = re.compile('Flash', re.I) print(p.findall('flash workingnotes Flash Workingnotes'))編譯正則表達(dá)式的時(shí)候,使用標(biāo)志re.I,匹配時(shí)候不區(qū)分大小寫,所以成功匹配flash和Flash。
2 匹配失敗
p = re.compile('Flash', re.I) print(p.findall('flah workingnotes Flah Workingnotes'))- finditer()函數(shù)
finditer()函數(shù)找到匹配成功的所有子串,并把它們作為一個(gè)迭代器返回
p = re.compile('Flash', re.I) print(p.finditer('flash workingnotes Flash Workingnotes'))p = re.compile('Flash', re.I) p1 = p.finditer('flash workingnotes Flash Workingnotes') for match in p1: print(match.group())- split()函數(shù)
split()函數(shù)基于正則表達(dá)式的模式分隔字符串,通過參數(shù)max指定最大分割數(shù)。
如果找不到匹配的字符串的話,不進(jìn)行分割。
1 使用非字母數(shù)字字符分割字符串
p = re.compile(r'W+') p.split('Flash,Workingnotes.flash+Workings FlashWorkingnotes')2 使用非字母數(shù)字字符分割字符串,限制最大分割次數(shù)為2
p = re.compile(r'W+') p.split('Flash,Workingnotes.flash+Workings FlashWorkingnotes', 2)3 匹配不到
p = re.compile(r'd') p.split('Flash,Workingnotes.flash+Workings FlashWorkingnotes', 2)- sub()函數(shù)和subn()函數(shù)
sub()函數(shù)和subn()函數(shù)用于搜索和替換,sub()函數(shù)找到匹配成功的所有子串,并將其用一個(gè)不同的字符串替換;subn()函數(shù)找到匹配成功的所有子串,并將其用一個(gè)不同的字符串替換,并且返回新的字符串和替換次數(shù)的元組。參數(shù)count可用于指定最大替換的次數(shù)。
1 使用gmy替換F(f)lash
p = re.compile(r'Flash', re.I) p.sub('gmy', ('flash workingnotes Flash Workingnotes'))2 使用gmy替換F(f)lash,替換次數(shù)為1次
p = re.compile(r'Flash', re.I) p.sub('gmy', ('flash workingnotes Flash Workingnotes'), 1)3 subn()函數(shù)與sub()函數(shù)一樣,返回的是包含新字符串和替換執(zhí)行次數(shù)的元組
p = re.compile(r'Flash', re.I) p.subn('gmy', ('flash workingnotes Flash Workingnotes'))p = re.compile(r'Flash', re.I) p.subn('gmy', ('flash workingnotes Flash Workingnotes'),1)6 總結(jié)
用了三部分介紹了Python之正則表達(dá)式re模塊,這里只是拋磚引玉,選擇性的介紹部分內(nèi)容,沒有介紹的可以參考https://docs.python.org/zh-cn/3/library/re.html。按照計(jì)劃,后面進(jìn)入到可視化內(nèi)容的介紹。
關(guān)于作者:從事風(fēng)控方面工作,數(shù)據(jù)科學(xué)愛好者,微信公眾號(hào)WorkingNotes,歡迎交流。
總結(jié)
以上是生活随笔為你收集整理的python findall函数_Ramp;Python Data Science系列:数据处理(11)Python正则表达式re模块(三)...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python源码文件_从源代码生成Pyt
- 下一篇: 月份对比_6月份钢坯市场或将高位回落