溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python的Re模塊怎么用

發(fā)布時(shí)間:2021-12-29 16:49:09 來源:億速云 閱讀:174 作者:小新 欄目:編程語言

這篇文章主要介紹Python的Re模塊怎么用,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

一、基礎(chǔ)語法總結(jié)

1.1、匹配單個(gè)字符

a . d D w W s S [...] [^...]

匹配單個(gè)字符(.)

規(guī)則:匹配除換行之外的任意字符

In [24]: re.findall("f.o","foo is not fao")  Out[24]: ['foo', 'fao']

匹配任意(非)數(shù)字字符(d D)

d [0-9] D [^0-9]

匹配任意(非)普通字符(w W)

w 普通字符 包括[_0-9A-Za-z]同時(shí)也包括漢字

W 非普通字符

匹配任意(非)空字符(s S)

s 匹配任意空字符 [ ]

S 匹配任意非空字符

匹配字符集合([...])

[A-Z][a-z][0-9][_123a-z]

匹配字符集([^...])

規(guī)則:字符集取非,除列出的字符之外的任意一個(gè)字符

[^abc] --> 除a b c之外任意字符

1.2、匹配多個(gè)字符

* 匹配0次或者多次

+ 匹配1次或者多次

? 匹配0次或者1次

{m} 匹配m次

{m,n} 匹配m次到n次區(qū)間內(nèi)的任意一次

1.3、匹配位置

^ 匹配開始位置

$ 匹配結(jié)束位置

A 匹配開始位置

Z 匹配結(jié)束位置

 匹配單詞邊界位置(一般用于首字母大寫的匹配)

B 匹配非單詞邊界問題

1.4、轉(zhuǎn)義

在正則表達(dá)式中有一類特殊字符需要轉(zhuǎn)移,只需要在特殊字符之間加上 表示轉(zhuǎn)移即可

. * + ? ^ $ [] {} () |

1.5、子組

使用() 可以為正則表達(dá)式建立內(nèi)部分組,子組為正則表達(dá)式的一部分,可以看做一個(gè)內(nèi)部整體。

In [61]: re.search(r"(https|http|ftp)://w+.w+.(com|cn)","https://www.baidu.com").group(0) Out[61]: 'https://www.baidu.com' In [62]: re.search(r"(https|http|ftp)://w+.w+.(com|cn)","https://www.baidu.com").group(1) Out[62]: 'https'

1.6、貪婪模式和非貪婪模式

正則表達(dá)式的重復(fù)匹配總是盡可能多的向后匹配更多的內(nèi)容。 貪婪模式包括:* + ? {m,n}

非貪婪模式:盡可能少的匹配內(nèi)容 貪婪模式轉(zhuǎn)換為非貪婪模式:*? +? ?? {m,n}?

In [106]: re.findall(r"ab+?","abbbbbbbb") Out[106]: ['ab'] In [107]: re.findall(r"ab??","abbbbbbbb") Out[107]: ['a']

二、Re模塊

Python的Re模塊怎么用

接下來我所有函數(shù)里面的參數(shù)解釋如下:

  • pattern:正則表達(dá)式

  • string:目標(biāo)字符串

  • pos:截取目標(biāo)字符串起始位置

  • endpose:截取目標(biāo)字符串結(jié)束位置

  • flags:功能標(biāo)志

  • replaceStr:替換的字符串

  • max:最多替換幾處(默認(rèn)替換全部)

有上圖我們看出來,接下來我們要將的Python中re模塊、regex對(duì)象、match對(duì)象三者之間是存在一定關(guān)系的。

  1. 鴻蒙官方戰(zhàn)略合作共建——HarmonyOS技術(shù)社區(qū)

  2. re模塊的compile方法返回一個(gè)regex對(duì)象

  3. re模塊和regex對(duì)象的finditer()、fullmatch()、match()、search()等方法返回一個(gè)match對(duì)象

  4. 他們分別有自己的屬性和方法

2.1、compile

regex = re.compile(pattern, flags = 0) # 生成正則表達(dá)式對(duì)象

2.2、findall

re.findall(pattern,string,pos,endpose) # 從目標(biāo)字符串中匹配所有符合條件的內(nèi)容

2.3、split

re.split(pattern,string,flags) #根據(jù)正則表達(dá)式對(duì)目標(biāo)字符串進(jìn)行分割 In [79]: re.split(r's+',"Hello World") Out[79]: ['Hello', 'World']

2.4、sub

re.sub(pattern,replaceStr,string,max,flags) In [80]: re.sub(r's+',"##","hello world") Out[80]: 'hello##world'

2.5、subn

re.subn(pattern,replaceStr,string,max,flags) #功能同sub,但是返回值返回替換后的字符串和替換了幾處 In [80]: re.sub(r's+',"##","hello world") Out[80]: ('hello##world',1)

2.6、finditer

re.finditer(pattern,string) #使用正則表達(dá)式匹配目標(biāo)字符串,返回一個(gè)match對(duì)象,match對(duì)象調(diào)用group()之后才能拿到值 In [87]: it = re.finditer(r'd+',"2014nianshiqiqngduo 08aoyun 512dizhen") In [88]: for i in it:  ....: print(i)  ....:  <_sre.SRE_Match object at 0x7f0639767920> <_sre.SRE_Match object at 0x7f0639767ac0> <_sre.SRE_Match object at 0x7f0639767920> In [93]: it = re.finditer(r'd+',"2014nianshiqiqngduo 08aoyun 512dizhen") In [94]: for i in it:  ....: print(i.group())  ....:  2014 08 512

2.7、fullmatch

fullmatch(pattern,string,flags) #完全匹配目標(biāo)字符串,相當(dāng)于加了^ 和 $

2.8、match

re.match(pattern,string,flags) #匹配目標(biāo)字符串開頭的位置

2.9、search

re.search(pattern,string,flags) # 正則表達(dá)式匹配目標(biāo)字符串,只匹配***處

三、一些練習(xí)題

3.1、匹配首字母大寫的單詞

import re f = open('test.txt') pattern= r'[A-Z][a-zA-Z]*s*' # pattern= r'[A-Z]S' L = [] for i in f:  L += re.findall(pattern,i) print(L)

test.txt文檔內(nèi)容如下:

Hello World -12.6 Nihao 123 How are you -12 1.24 asdk 34%, 占比 1/2 2003 - 2005./%

3.2、匹配數(shù)字(正數(shù)、負(fù)數(shù)、小數(shù)、百分?jǐn)?shù)、分?jǐn)?shù))

import re pattern = "-?d+((/?d+)|((.)?d+)|((%)?))" f = open('test.txt') l = [] for line in f:  l += re.finditer(pattern,line) for i in l:  print(i.group())

以上是“Python的Re模塊怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI