国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

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

2020-02-15 21:47:39
字體:
來源:轉載
供稿:網友

本文實例講述了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)
該標志通過給予你更靈活的格式以便你將正則表達式寫得更易于理解。

示例:

import recontent = '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 需要匹配的字符串
import repattern = 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 restr = '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)) # 匹配第一組,helloprint('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))            
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巨野县| 温泉县| 桐柏县| 南皮县| 噶尔县| 喀喇沁旗| 福建省| 富平县| 汉阴县| 凤庆县| 新建县| 嘉黎县| 瓦房店市| 忻城县| 方山县| 东山县| 离岛区| 象山县| 汉源县| 巴彦淖尔市| 贵溪市| 怀仁县| 洛扎县| 江永县| 郸城县| 广德县| 阿荣旗| 陕西省| 伊春市| 吴忠市| 义马市| 万荣县| 廉江市| 叶城县| 珠海市| 秭归县| 双牌县| 甘南县| 玉龙| 普定县| 麻城市|