日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python findall函数_Ramp;Python Data Science系列:数据处理(11)Python正则表达式re模块(三)...

發布時間:2024/1/23 python 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python findall函数_Ramp;Python Data Science系列:数据处理(11)Python正则表达式re模块(三)... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

使用正則表達式進行匹配,可以直接調用模塊級函數,如match()、search()、findall()等,函數第一個參數是匹配的正則表達式,第二個參數則為要匹配的字符串。也可以使用re.compile()先將正則表達式編譯成RegexObject對象,然后再調用RegexObject對象的方法,參數為要匹配的字符串。例如:

re.search(r'flash', 'Flash_WorkingNotes', re.I).group()

等價于

p = re.compile(r'flash', re.I) p.search('Flash_WorkingNotes').group()

如果匹配的正則表達式只用一次,模塊級函數使用起來很方便;若項目中包含多個正則表達式或者一個正則表達式被多次使用,編譯成RegexObject對象更方便一些。以下內容,先編譯成正則表達式對象,然后再調用這些對象的方法。

5.2.3 RegexObject的方法和MatchObject的方法

re模塊提供了一個正則表達式引擎接口,可以將正則表達式編譯成對象并用它們進行匹配。使用re.compile()將正則表達式編譯成RegexObject對象,有對象就有方法可以調用,RegexObject對象常用方法有match()、search()、findall()、finditer()、split()、sub()以及subn()。

match()和search()匹配成功的話返回一個MatchObject實例,findall()、split()、sub()以及subn()返回一個列表,finditer()返回一個迭代器。

  • match()函數
    match()函數檢查RE是否在字符串開始處匹配,match()函數只返回一次成功的匹配,從0開始,如果不是從0匹配成功,返回None。如果匹配成功,返回一個MatchObject 對象,可以通過group方法獲取匹配成功的整個字符串。

1 匹配成功

p = re.compile('Flash', re.I) p.match('flash workingnotes')

匹配成功的話,返回一個MatchObject對象,可以調用MatchObject的方法。

2 使用group()函數返回匹配成功的整個字符串

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()返回匹配結束的位置

p = re.compile('Flash', re.I) p.match('flash workingnotes').end()

5 使用span()返回一個元組包含匹配(開始,結束)的位置

p = re.compile('Flash', re.I) p.match('flash workingnotes').span()

6 匹配失敗

p = re.compile('Flash', re.I) print(p.match('workingnotes flash'))

  • search()函數

search()函數檢查整個字符串,匹配成功,返回一個匹配對象MatchObject,沒有匹配成功返回None

1 匹配成功

p = re.compile('Flash', re.I) print(p.search('workingnotes flash'))

2 使用group()函數返回匹配成功的整個字符串

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()返回匹配結束的位置

p = re.compile('Flash', re.I) print(p.search('workingnotes flash').end())

5 使用span()返回一個元組包含匹配(開始,結束)的位置

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()函數

findall()函數找到匹配成功的所有子串,并把它們作為一個列表返回,若沒有匹配成功,返回空列表

1 匹配成功

p = re.compile('Flash', re.I) print(p.findall('flash workingnotes Flash Workingnotes'))

編譯正則表達式的時候,使用標志re.I,匹配時候不區分大小寫,所以成功匹配flash和Flash。

2 匹配失敗

p = re.compile('Flash', re.I) print(p.findall('flah workingnotes Flah Workingnotes'))

  • finditer()函數

finditer()函數找到匹配成功的所有子串,并把它們作為一個迭代器返回

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()函數

split()函數基于正則表達式的模式分隔字符串,通過參數max指定最大分割數。

如果找不到匹配的字符串的話,不進行分割。

1 使用非字母數字字符分割字符串

p = re.compile(r'W+') p.split('Flash,Workingnotes.flash+Workings FlashWorkingnotes')

2 使用非字母數字字符分割字符串,限制最大分割次數為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()函數和subn()函數
    sub()函數和subn()函數用于搜索和替換,sub()函數找到匹配成功的所有子串,并將其用一個不同的字符串替換;subn()函數找到匹配成功的所有子串,并將其用一個不同的字符串替換,并且返回新的字符串和替換次數的元組。參數count可用于指定最大替換的次數。

1 使用gmy替換F(f)lash

p = re.compile(r'Flash', re.I) p.sub('gmy', ('flash workingnotes Flash Workingnotes'))

2 使用gmy替換F(f)lash,替換次數為1次

p = re.compile(r'Flash', re.I) p.sub('gmy', ('flash workingnotes Flash Workingnotes'), 1)

3 subn()函數與sub()函數一樣,返回的是包含新字符串和替換執行次數的元組

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 總結

用了三部分介紹了Python之正則表達式re模塊,這里只是拋磚引玉,選擇性的介紹部分內容,沒有介紹的可以參考https://docs.python.org/zh-cn/3/library/re.html。按照計劃,后面進入到可視化內容的介紹。


關于作者:從事風控方面工作,數據科學愛好者,微信公眾號WorkingNotes,歡迎交流。

總結

以上是生活随笔為你收集整理的python findall函数_Ramp;Python Data Science系列:数据处理(11)Python正则表达式re模块(三)...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。