Python3中正則模塊re.compile、re.match及re.search函數(shù)用法詳解
本文實(shí)例講述了Python3中正則模塊re.compile、re.match及re.search函數(shù)用法。分享給大家供大家參考,具體如下:
re模塊 re.compile、re.match、 re.search
正則匹配的時(shí)候,第一個(gè)字符是 r,表示 raw string 原生字符,意在聲明字符串中間的特殊字符不用轉(zhuǎn)義。
比如表示 ‘\n',可以寫 r'\n',或者不適用原生字符 ‘\n'。
推薦使用 re.match
re.compile() 函數(shù)
編譯正則表達(dá)式模式,返回一個(gè)對象。可以把常用的正則表達(dá)式編譯成正則表達(dá)式對象,方便后續(xù)調(diào)用及提高效率。
re.compile(pattern, flags=0)
- pattern 指定編譯時(shí)的表達(dá)式字符串
- flags 編譯標(biāo)志位,用來修改正則表達(dá)式的匹配方式。支持 re.L|re.M 同時(shí)匹配
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字符集解析字符。這個(gè)標(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 返回的是一個(gè) list 對象
<class '_sre.SRE_Pattern'>
<class 'list'>
['wang', 'WANG']
re.match() 函數(shù)
總是從字符串‘開頭曲匹配',并返回匹配的字符串的 match 對象 <class '_sre.SRE_Match'>。
re.match(pattern, string[, flags=0])
- pattern 匹配模式,由 re.compile 獲得
- string 需要匹配的字符串
import re pattern = re.compile(r'hello') a = re.match(pattern, 'hello world') b = re.match(pattern, 'world hello') c = re.match(pattern, 'hell') d = re.match(pattern, 'hello ') if a: print(a.group()) else: print('a 失敗') if b: print(b.group()) else: print('b 失敗') if c: print(c.group()) else: print('c 失敗') if d: print(d.group()) else: print('d 失敗')
hello
b 失敗
c 失敗
hello
match 的方法和屬性
import re str = 'hello world! hello python' pattern = re.compile(r'(?P<first>hell\w)(?P<symbol>\s)(?P<last>.*ld!)') # 分組,0 組是整個(gè) hello world!, 1組 hello,2組 ld! match = re.match(pattern, str) print('group 0:', match.group(0)) # 匹配 0 組,整個(gè)字符串 print('group 1:', match.group(1)) # 匹配第一組,hello print('group 2:', match.group(2)) # 匹配第二組,空格 print('group 3:', match.group(3)) # 匹配第三組,ld! print('groups:', match.groups()) # groups 方法,返回一個(gè)包含所有分組匹配的元組 print('start 0:', match.start(0), 'end 0:', match.end(0)) # 整個(gè)匹配開始和結(jié)束的索引值 print('start 1:', match.start(1), 'end 1:', match.end(1)) # 第一組開始和結(jié)束的索引值 print('start 2:', match.start(1), 'end 2:', match.end(2)) # 第二組開始和結(jié)束的索引值 print('pos 開始于:', match.pos) print('endpos 結(jié)束于:', match.endpos) # string 的長度 print('lastgroup 最后一個(gè)被捕獲的分組的名字:', match.lastgroup) print('lastindex 最后一個(gè)分組在文本中的索引:', match.lastindex) print('string 匹配時(shí)候使用的文本:', match.string) print('re 匹配時(shí)候使用的 Pattern 對象:', match.re) print('span 返回分組匹配的 index (start(group),end(group)):', match.span(2))
返回結(jié)果:
group 0: hello world!
group 1: hello
group 2:
group 3: world!
groups: ('hello', ' ', 'world!')
start 0: 0 end 0: 12
start 1: 0 end 1: 5
start 2: 0 end 2: 6
pos 開始于: 0
endpos 結(jié)束于: 25
lastgroup 最后一個(gè)被捕獲的分組的名字: last
lastindex 最后一個(gè)分組在文本中的索引: 3
string 匹配時(shí)候使用的文本: hello world! hello python
re 匹配時(shí)候使用的 Pattern 對象: re.compile('(?P<first>hell\\w)(?P<symbol>\\s)(?P<last>.*ld!)')
span 返回分組匹配的 index (start(group),end(group)): (5, 6)
re.search 函數(shù)
對整個(gè)字符串進(jìn)行搜索匹配,返回第一個(gè)匹配的字符串的 match 對象。
re.search(pattern, string[, flags=0])
- pattern 匹配模式,由 re.compile 獲得
- string 需要匹配的字符串
import re str = 'say hello world! hello python' pattern = re.compile(r'(?P<first>hell\w)(?P<symbol>\s)(?P<last>.*ld!)') # 分組,0 組是整個(gè) hello world!, 1組 hello,2組 ld! search = re.search(pattern, str) print('group 0:', search.group(0)) # 匹配 0 組,整個(gè)字符串 print('group 1:', search.group(1)) # 匹配第一組,hello print('group 2:', search.group(2)) # 匹配第二組,空格 print('group 3:', search.group(3)) # 匹配第三組,ld! print('groups:', search.groups()) # groups 方法,返回一個(gè)包含所有分組匹配的元組 print('start 0:', search.start(0), 'end 0:', search.end(0)) # 整個(gè)匹配開始和結(jié)束的索引值 print('start 1:', search.start(1), 'end 1:', search.end(1)) # 第一組開始和結(jié)束的索引值 print('start 2:', search.start(1), 'end 2:', search.end(2)) # 第二組開始和結(jié)束的索引值 print('pos 開始于:', search.pos) print('endpos 結(jié)束于:', search.endpos) # string 的長度 print('lastgroup 最后一個(gè)被捕獲的分組的名字:', search.lastgroup) print('lastindex 最后一個(gè)分組在文本中的索引:', search.lastindex) print('string 匹配時(shí)候使用的文本:', search.string) print('re 匹配時(shí)候使用的 Pattern 對象:', search.re) print('span 返回分組匹配的 index (start(group),end(group)):', search.span(2))
注意 re.search 和 re.match 匹配的 str 的區(qū)別
打印結(jié)果:
group 0: hello world!
group 1: hello
group 2:
group 3: world!
groups: ('hello', ' ', 'world!')
start 0: 4 end 0: 16
start 1: 4 end 1: 9
start 2: 4 end 2: 10
pos 開始于: 0
endpos 結(jié)束于: 29
lastgroup 最后一個(gè)被捕獲的分組的名字: last
lastindex 最后一個(gè)分組在文本中的索引: 3
string 匹配時(shí)候使用的文本: say hello world! hello python
re 匹配時(shí)候使用的 Pattern 對象: re.compile('(?P<first>hell\\w)(?P<symbol>\\s)(?P<last>.*ld!)')
span 返回分組匹配的 index (start(group),end(group)): (9, 10)
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è)計(jì)有所幫助。
相關(guān)文章
Python pandas實(shí)現(xiàn)excel工作表合并功能詳解
這篇文章主要介紹了Python pandas實(shí)現(xiàn)excel工作表合并功能以及相關(guān)實(shí)例代碼,需要的朋友們參考學(xué)習(xí)下。2019-08-08Python?numpy下幾種fft函數(shù)的使用方式
numpy中有一個(gè)fft的庫,scipy中也有一個(gè)fftpack的庫,各自都有fft函數(shù),兩者的用法基本是一致的,下面這篇文章主要給大家介紹了關(guān)于Python?numpy下幾種fft函數(shù)的使用方式,需要的朋友可以參考下2022-08-08人工智能學(xué)習(xí)Pytorch張量數(shù)據(jù)類型示例詳解
這篇文章主要為大家介紹了人工智能學(xué)習(xí)Pytorch張量數(shù)據(jù)類型的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11matplotlib調(diào)整子圖間距,調(diào)整整體空白的方法
今天小編就為大家分享一篇matplotlib調(diào)整子圖間距,調(diào)整整體空白的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-08-08python使用openpyxl打開及讀取excel表格過程
openpyxl是一個(gè)Python庫,用于讀寫Excel?2010?xlsx/xlsm文件,它允許你輕松工作與Excel表格,進(jìn)行數(shù)據(jù)處理和分析,支持讀取、創(chuàng)建和修改Excel文件,甚至可以在Excel中插入圖表等,安裝非常簡單,只需要使用pip命令即可2024-09-09