Python3正則匹配re.split,re.finditer及re.findall函數(shù)用法詳解
本文實(shí)例講述了Python3正則匹配re.split,re.finditer及re.findall函數(shù)用法。分享給大家供大家參考,具體如下:
re.split re.finditer re.findall
@(python3)
re.compile() 函數(shù)
編譯正則表達(dá)式模式,返回一個對象??梢园殉S玫恼齽t表達(dá)式編譯成正則表達(dá)式對象,方便后續(xù)調(diào)用及提高效率。
re 模塊最離不開的就是 re.compile 函數(shù)。其他函數(shù)都依賴于 compile 創(chuàng)建的 正則表達(dá)式對象
re.compile(pattern, flags=0)
- pattern 指定編譯時的表達(dá)式字符串
- flags 編譯標(biāo)志位,用來修改正則表達(dá)式的匹配方式。支持 re.L|re.M 同時匹配
flags 標(biāo)志位參數(shù)
re.I(re.IGNORECASE)
使匹配對大小寫不敏感
re.L(re.LOCAL)
做本地化識別(locale-aware)匹配
re.M(re.MULTILINE)
多行匹配,影響 ^ 和 $
re.S(re.DOTALL)
使 . 匹配包括換行在內(nèi)的所有字符
re.U(re.UNICODE)
根據(jù)Unicode字符集解析字符。這個標(biāo)志影響 \w, \W, \b, \B.
re.X(re.VERBOSE)
該標(biāo)志通過給予你更靈活的格式以便你將正則表達(dá)式寫得更易于理解。
示例:
import re content = 'Citizen wang , always fall in love with neighbour,WANG' rr = re.compile(r'wan\w', re.I) # 不區(qū)分大小寫 print(type(rr)) a = rr.findall(content) print(type(a)) print(a)
findall 返回的是一個 list 對象
<class '_sre.SRE_Pattern'>
<class 'list'>
['wang', 'WANG']
re.split 函數(shù)
按照指定的 pattern 格式,分割 string 字符串,返回一個分割后的列表。
re.split(pattern, string, maxsplit=0, flags=0)
- pattern compile 生成的正則表達(dá)式對象,或者自定義也可
- string 要匹配的字符串
- maxsplit 指定最大分割次數(shù),不指定將全部分割
import re str = 'say hello world! hello python' str_nm = 'one1two2three3four4' pattern = re.compile(r'(?P<space>\s)') # 創(chuàng)建一個匹配空格的正則表達(dá)式對象 pattern_nm = re.compile(r'(?P<space>\d+)') # 創(chuàng)建一個匹配空格的正則表達(dá)式對象 match = re.split(pattern, str) match_nm = re.split(pattern_nm, str_nm, maxsplit=1) print(match) print(match_nm)
結(jié)果:
['say', ' ', 'hello', ' ', 'world!', ' ', 'hello', ' ', 'python']
['one', '1', 'two2three3four4']
re.findall() 方法
返回一個包含所有匹配到的字符串的列表。
- pattern 匹配模式,由 re.compile 獲得
- string 需要匹配的字符串
import re str = 'say hello world! hello python' pattern = re.compile(r'(?P<first>h\w)(?P<symbol>l+)(?P<last>o\s)') # 分組,0 組是整個 world!, 1組 or,2組 ld! match = re.findall(pattern, str) print(match)
結(jié)果
[('he', 'll', 'o '), ('he', 'll', 'o ')]
re.finditer 、re.findall
re.finditer(pattern, string[, flags=0])
re.findall(pattern, string[, flags=0])
- pattern compile 生成的正則表達(dá)式對象,或者自定義也可
- string 要匹配的字符串
findall 返回一個包含所有匹配到的字符的列表,列表類以元組的形式存在。
finditer 返回一個可迭代對象。
示例一:
pattern = re.compile(r'\d+@\w+.com') #通過 re.compile 獲得一個正則表達(dá)式對象 result_finditer = re.finditer(pattern, content) print(type(result_finditer)) print(result_finditer) # finditer 得到的結(jié)果是個可迭代對象 for i in result_finditer: # i 本身也是可迭代對象,所以下面要使用 i.group() print(i.group()) result_findall = re.findall(pattern, content) print(type(result_findall)) # findall 得到的是一個列表 print(result_findall) for p in result_finditer: print(p)
輸出結(jié)果:
<class 'callable_iterator'>
<callable_iterator object at 0x10545ec88>
123456@163.com
234567@163.com
345678@163.com
<class 'list'>
['123456@163.com', '234567@163.com', '345678@163.com']
由結(jié)果可知:finditer 得到的是可迭代對象,finfdall 得到的是一個列表。
示例二:
import re content = '''email:123456@163.com email:234567@163.com email:345678@163.com ''' pattern = re.compile(r'(?P<number>\d+)@(?P<mail_type>\w+).com') result_finditer = re.finditer(pattern, content) print(type(result_finditer)) print(result_finditer) iter_dict = {} # 把最后得到的結(jié)果 for i in result_finditer: print('郵箱號碼是:', i.group(1),'郵箱類型是:',i.group(2)) number = i.group(1) mail_type = i.group(2) iter_dict.setdefault(number, mail_type) # 使用 dict.setdefault 創(chuàng)建了一個字典 print(iter_dict) print('+++++++++++++++++++++++++++++++') result_findall = re.findall(pattern, content) print(result_findall) print(type(result_findall))
輸出結(jié)果:
<class 'callable_iterator'>
<callable_iterator object at 0x104c5cbe0>
郵箱號碼是: 123456 郵箱類型是: 163
郵箱號碼是: 234567 郵箱類型是: 163
郵箱號碼是: 345678 郵箱類型是: 163
{'123456': '163', '234567': '163', '345678': '163'}
+++++++++++++++++++++++++++++++
[('123456', '163'), ('234567', '163'), ('345678', '163')]
<class 'list'>
finditer 得到的可迭代對象 i,也可以使用 lastindex,lastgroup 方法。
print('lastgroup 最后一個被捕獲的分組的名字',i.lastgroup)
findall 當(dāng)正則沒有分組,返回就是正則匹配。
re.findall(r"\d+@\w+.com", content) ['2345678@163.com', '2345678@163.com', '345678@163.com']
有一個分組返回的是分組的匹配
re.findall(r"(\d+)@\w+.com", content) ['2345678', '2345678', '345678']
多個分組時,將結(jié)果作為 元組,一并存入到 列表中。
re.findall(r"(\d+)@(\w+).com", content) [('2345678', '163'), ('2345678', '163'), ('345678', '163')]
PS:這里再為大家提供2款非常方便的正則表達(dá)式工具供大家參考使用:
JavaScript正則表達(dá)式在線測試工具:
http://tools.jb51.net/regex/javascript
正則表達(dá)式在線生成工具:
http://tools.jb51.net/regex/create_reg
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python 將字符串轉(zhuǎn)換成字典dict的各種方式總結(jié)
下面小編就為大家分享一篇python 將字符串轉(zhuǎn)換成字典dict的各種方式總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03python爬蟲之BeautifulSoup 使用select方法詳解
本篇文章主要介紹了python爬蟲之BeautifulSoup 使用select方法詳解,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10對python中raw_input()和input()的用法詳解
下面小編就為大家分享一篇對python中raw_input()和input()的用法詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04關(guān)于Numpy中argsort()函數(shù)的用法解讀
這篇文章主要介紹了關(guān)于Numpy中argsort()函數(shù)的用法解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)從任意長度的可迭代對象中分解元素操作示例
這篇文章主要介紹了Python 數(shù)據(jù)結(jié)構(gòu)與算法 從任意長度的可迭代象中分解元素操作,結(jié)合實(shí)例形式分析了Python使用*表達(dá)式針對可迭代對象的分解操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-02-02Python實(shí)現(xiàn)堡壘機(jī)模式下遠(yuǎn)程命令執(zhí)行操作示例
這篇文章主要介紹了Python實(shí)現(xiàn)堡壘機(jī)模式下遠(yuǎn)程命令執(zhí)行操作,結(jié)合實(shí)例形式分析了Python堡壘機(jī)模式執(zhí)行遠(yuǎn)程命令的原理與相關(guān)操作技巧,需要的朋友可以參考下2019-05-05利用python檢查磁盤空間使用情況的代碼實(shí)現(xiàn)
本文將向讀者展示如何利用Python編寫自動化腳本,以檢查磁盤空間使用情況,無論你是經(jīng)驗(yàn)豐富的系統(tǒng)管理員,還是對Python自動化充滿興趣的開發(fā)者,本文都將為你提供實(shí)用的腳本示例和詳細(xì)的解析步驟,幫助你快速掌握磁盤空間監(jiān)控的自動化方法,需要的朋友可以參考下2024-08-08