1. 程式人生 > >Python3中正則模組re.compile、re.match及re.search函式用法詳解

Python3中正則模組re.compile、re.match及re.search函式用法詳解

本文例項講述了Python3中正則模組re.compile、re.match及re.search函式用法。分享給大家供大家參考,具體如下:

re模組 re.compile、re.match、 re.search

re 模組官方說明文件

正則匹配的時候,第一個字元是 r,表示 raw string 原生字元,意在宣告字串中間的特殊字元不用轉義。

比如表示 ‘\n',可以寫 r'\n',或者不適用原生字元 ‘\n'。

推薦使用 re.match

re.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)
該標誌通過給予你更靈活的格式以便你將正則表示式寫得更易於理解。

示例:

?
1 2 3 4 5 6 7 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 物件

<class '_sre.SRE_Pattern'>
<class 'list'>
['wang', 'WANG']

re.match() 函式

總是從字串‘開頭曲匹配',並返回匹配的字串的 match 物件 <class '_sre.SRE_Match'>。

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

  • pattern 匹配模式,由 re.compile 獲得
  • string 需要匹配的字串
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 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 的方法和屬性

參考連結

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import re str = 'hello world! hello python' pattern = re. compile (r '(?P<first>hell\w)(?P<symbol>\s)(?P<last>.*ld!)' ) # 分組,0 組是整個 hello world!, 1組 hello,2組 ld! match = re.match(pattern, str ) print ( 'group 0:' , match.group( 0 )) # 匹配 0 組,整個字串 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 方法,返回一個包含所有分組匹配的元組 print ( 'start 0:' , match.start( 0 ), 'end 0:' , match.end( 0 )) # 整個匹配開始和結束的索引值 print ( 'start 1:' , match.start( 1 ), 'end 1:' , match.end( 1 )) # 第一組開始和結束的索引值 print ( 'start 2:' , match.start( 1 ), 'end 2:' , match.end( 2 )) # 第二組開始和結束的索引值 print ( 'pos 開始於:' , match.pos) print ( 'endpos 結束於:' , match.endpos) # string 的長度 print ( 'lastgroup 最後一個被捕獲的分組的名字:' , match.lastgroup) print ( 'lastindex 最後一個分組在文字中的索引:' , match.lastindex) print ( 'string 匹配時候使用的文字:' , match.string) print ( 're 匹配時候使用的 Pattern 物件:' , match.re) print ( 'span 返回分組匹配的 index (start(group),end(group)):' , match.span( 2 ))

返回結果:

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 結束於: 25
lastgroup 最後一個被捕獲的分組的名字: last
lastindex 最後一個分組在文字中的索引: 3
string 匹配時候使用的文字: hello world! hello python
re 匹配時候使用的 Pattern 物件: re.compile('(?P<first>hell\\w)(?P<symbol>\\s)(?P<last>.*ld!)')
span 返回分組匹配的 index (start(group),end(group)): (5, 6)

re.search 函式

對整個字串進行搜尋匹配,返回第一個匹配的字串的 match 物件。

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

  • pattern 匹配模式,由 re.compile 獲得
  • string 需要匹配的字串
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 import re str = 'say hello world! hello python' pattern = re. compile (r '(?P<first>hell\w)(?P<symbol>\s)(?P<last>.*ld!)' ) # 分組,0 組是整個 hello world!, 1組 hello,2組 ld! search = re.search(pattern, str ) print ( 'group 0:' , search.group( 0 )) # 匹配 0 組,整個字串 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 方法,返回一個包含所有分組匹配的元組 print ( 'start 0:' , search.start( 0 ), 'end 0:' , search.end( 0 )) # 整個匹配開始和結束的索引值 print ( 'start 1:' , search.start( 1 ), 'end 1:' , search.end( 1 )) # 第一組開始和結束的索引值 print ( 'start 2:' , search.start( 1 ), 'end 2:' , search.end( 2 )) # 第二組開始和結束的索引值 print ( 'pos 開始於:' , search.pos) print ( 'endpos 結束於:' , search.endpos) # string 的長度 print ( 'lastgroup 最後一個被捕獲的分組的名字:' , search.lastgroup) print ( 'lastindex 最後一個分組在文字中的索引:' , search.lastindex) print ( 'string 匹配時候使用的文字:' , search.string) print ( 're 匹配時候使用的 Pattern 物件:' , search.re) print ( 'span 返回分組匹配的 index (start(group),end(group)):' , search.span( 2 ))

注意 re.search 和 re.match 匹配的 str 的區別

列印結果:

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 結束於: 29
lastgroup 最後一個被捕獲的分組的名字: last
lastindex 最後一個分組在文字中的索引: 3
string 匹配時候使用的文字: say hello world! hello python
re 匹配時候使用的 Pattern 物件: re.compile('(?P<first>hell\\w)(?P<symbol>\\s)(?P<last>.*ld!)')
span 返回分組匹配的 index (start(group),end(group)): (9, 10)

PS:這裡再為大家提供2款非常方便的正則表示式工具供大家參考使用:

JavaScript正則表示式線上測試工具:
http://tools.jb51.net/regex/javascript

正則表示式線上生成工具:
http://tools.jb51.net/regex/create_reg

更多關於Python相關內容可檢視本站專題:《Python正則表示式用法總結》、《Python資料結構與演算法教程》、《Python函式使用技巧總結》、《Python字串操作技巧彙總》、《Python入門與進階經典教程》及《Python檔案與目錄操作技巧彙總

希望本文所述對大家Python程式設計有所幫助。