最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2018年06月11日 14:24:29   作者:Citizen_Wang  
這篇文章主要介紹了Python3中正則模塊re.compile、re.match及re.search函數用法,結合實例形式較為詳細的分析了re模塊 中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() 函數

編譯正則表達式模式,返回一個對象??梢园殉S玫恼齽t表達式編譯成正則表達式對象,方便后續(xù)調用及提高效率。

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) # 不區(qū)分大小寫
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 需要匹配的字符串
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 組是整個 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 需要匹配的字符串
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 的區(qū)別

打印結果:

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程序設計有所幫助。

相關文章

  • Python實現搶購IPhone手機

    Python實現搶購IPhone手機

    這篇文章主要為大家詳細介紹了Python實現搶購IPhone手機,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • python學習實操案例(三)

    python學習實操案例(三)

    這篇文章主要介紹了python學習實操案例,循環(huán)輸出26個字母對應的ASCII碼值、模擬用戶登錄、猜數游戲練習等案例,需要的小伙伴可以參考一下,希望對你有一定的幫助
    2022-02-02
  • python正則表達式去除兩個特殊字符間的內容方法

    python正則表達式去除兩個特殊字符間的內容方法

    今天小編就為大家分享一篇python正則表達式去除兩個特殊字符間的內容方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-12-12
  • 在python3.9下如何安裝scrapy的方法

    在python3.9下如何安裝scrapy的方法

    這篇文章主要介紹了在python3.9下如何安裝scrapy的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-02-02
  • 四種Python機器學習超參數搜索方法總結

    四種Python機器學習超參數搜索方法總結

    在建模時模型的超參數對精度有一定的影響,而設置和調整超參數的取值,往往稱為調參。本文將演示在sklearn中支持的四種基礎超參數搜索方法,需要的可以參考一下
    2022-11-11
  • 200 行python 代碼實現 2048 游戲

    200 行python 代碼實現 2048 游戲

    2048這個小游戲大家都不陌生,應該都玩過,之前已經在網上見過各個版本的2048實現了,有JAVA、HTML5等,今天我就給大家來一個我200 行python 代碼實現的2048 游戲,感興趣的朋友一起看看吧
    2018-01-01
  • Python數據清洗之利用pandas篩選數據詳解

    Python數據清洗之利用pandas篩選數據詳解

    這篇文章主要介紹了Python數據清洗之利用pandas篩選數據詳解,Pandas是一個用于數據分析和處理的Python庫,它提供了高效的數據結構和數據分析工具,使得數據的清洗、轉換、分析和可視化變得更加容易和靈活,需要的朋友可以參考下
    2023-08-08
  • Python Dict 到 Dataclass實現高效數據訪問與管理的兩種方式(推薦)

    Python Dict 到 Dataclass實現高效數據訪問與管理的兩種方式(推薦)

    本文介紹了Python中的字典和DataClass兩種數據結構,并探討了如何將字典轉換為DataClass,字典適用于鍵值對存儲,感興趣的朋友一起看看吧
    2024-12-12
  • pytorch 中transforms的使用詳解

    pytorch 中transforms的使用詳解

    本文主要介紹了pytorch中transforms的使用詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • Python中快速掌握Data Frame的常用操作

    Python中快速掌握Data Frame的常用操作

    這篇文章主要介紹了Python中快速掌握Data Frame的常用操作,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-03-03

最新評論

沾化县| 缙云县| 城市| 榕江县| 河北区| 阿坝县| 苏尼特右旗| 华蓥市| 固阳县| 遂平县| 灵台县| 陵水| 朝阳县| 长泰县| 大新县| 定边县| 海原县| 涿州市| 秦皇岛市| 肇州县| 拜城县| 盐池县| 福海县| 洪洞县| 南乐县| 上杭县| 通海县| 泸州市| 土默特右旗| 禹州市| 甘泉县| 高要市| 武夷山市| 满洲里市| 永吉县| 曲阜市| 濉溪县| 斗六市| 理塘县| 沙湾县| 南和县|