Python3正則匹配re.split,re.finditer及re.findall函數用法詳解

本文實例講述了Python3正則匹配re.split,re.finditer及re.findall函數用法。分享給大家供大家參考,具體如下:

re.split re.finditer re.findall

@(python3)
re.compile() 函數

編譯正則表達式模式,返回一個對象。可以把常用的正則表達式編譯成正則表達式對象,方便後續調用及提高效率。

re 模塊最離不開的就是 re.compile 函數。其他函數都依賴於 compile 創建的 正則表達式對象

re.compile(pattern, flags=0)

pattern 指定編譯時的表達式字符串
flags 編譯標誌位,用來修改正則表達式的匹配方式。支持 re.L|re.M 同時匹配
flags 標誌位參數

re.I(re.IGNORECASE)
使匹配對大小寫不敏感

re.L(re.LOCAL)
做本地化識別(locale-aware)匹配

re.M(re.MULTILINE)
多行匹配,影響 ^ 和 $

re.S(re.DOTALL)
使 . 匹配包括換行在內的所有字符

re.U(re.UNICODE)
根據Unicode字符集解析字符。這個標誌影響 \w, \W, \b, \B.

re.X(re.VERBOSE)
該標誌通過給予你更靈活的格式以便你將正則表達式寫得更易於理解。

示例:

import re
content = 'Citizen wang , always fall in love with neighbour,WANG'
rr = re.compile(r'wan\w', re.I) # 不區分大小寫
print(type(rr))
a = rr.findall(content)
print(type(a))
print(a)

findall 返回的是一個 list 對象
在這裏插入圖片描述
re.split 函數

按照指定的 pattern 格式,分割 string 字符串,返回一個分割後的列表。

re.split(pattern, string, maxsplit=0, flags=0)

pattern compile 生成的正則表達式對象,或者自定義也可
string 要匹配的字符串
maxsplit 指定最大分割次數,不指定將全部分割

import re
str = 'say hello world! hello python'
str_nm = 'one1two2three3four4'
pattern = re.compile(r'(?P<space>\s)') # 創建一個匹配空格的正則表達式對象
pattern_nm = re.compile(r'(?P<space>\d+)') # 創建一個匹配空格的正則表達式對象
match = re.split(pattern, str)
match_nm = re.split(pattern_nm, str_nm, maxsplit=1)
print(match)
print(match_nm)

結果:
在這裏插入圖片描述
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)

結果

[('he', 'll', 'o '), ('he', 'll', 'o ')]

re.finditer 、re.findall

re.finditer(pattern, string[, flags=0])
re.findall(pattern, string[, flags=0])

pattern compile 生成的正則表達式對象,或者自定義也可
string 要匹配的字符串
findall 返回一個包含所有匹配到的字符的列表,列表類以元組的形式存在。

finditer 返回一個可迭代對象。

示例一:

pattern = re.compile(r'\d+@\w+.com') #通過 re.compile 獲得一個正則表達式對象
result_finditer = re.finditer(pattern, content)
print(type(result_finditer))
print(result_finditer) # finditer 得到的結果是個可迭代對象
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)

輸出結果:在這裏插入圖片描述
由結果可知:finditer 得到的是可迭代對象,finfdall 得到的是一個列表。

示例二

import re
content = '''email:[email protected]
email:[email protected]
email:[email protected]
'''
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 = {} # 把最後得到的結果
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 創建了一個字典
print(iter_dict)
print('+++++++++++++++++++++++++++++++')
result_findall = re.findall(pattern, content)
print(result_findall)
print(type(result_findall))

輸出結果:

<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 當正則沒有分組,返回就是正則匹配。

re.findall(r"\d+@\w+.com", content)
['[email protected]', '[email protected]', '[email protected]']

有一個分組返回的是分組的匹配

re.findall(r"(\d+)@\w+.com", content)
['2345678', '2345678', '345678']

多個分組時,將結果作爲 元組,一併存入到 列表中。

re.findall(r"(\d+)@(\w+).com", content)
[('2345678', '163'), ('2345678', '163'), ('345678', '163')]

推薦我們的python學習基地,點擊進入,看老程序是如何學習的!從基礎的python腳本、爬蟲、django、數據挖掘等編程技術,工作經驗,還有前輩精心爲學習python的小夥伴整理零基礎到項目實戰的資料,!每天都有程序員定時講解Python技術,分享一些學習的方法和需要留意的小細節

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章