python正則表達(dá)式-創(chuàng)新互聯(lián)

python使用re模塊提供了正則表達(dá)式的處理能力

常量

創(chuàng)新互聯(lián)是一家專(zhuān)注于成都做網(wǎng)站、網(wǎng)站制作與策劃設(shè)計(jì),五峰網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專(zhuān)注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專(zhuān)業(yè)建站公司;建站業(yè)務(wù)涵蓋:五峰等地區(qū)。五峰做網(wǎng)站價(jià)格咨詢(xún):18982081108
常量                                     說(shuō)明
re.M、re.MULTILINE          多行模式  
re.S、re.DOTALL                單行模式
re.I、re.IGNORECASE       忽略大小寫(xiě)
re.X、re.VERBOSE            忽略表達(dá)式中的空白字符
使用 | 位或 運(yùn)算開(kāi)啟多種選項(xiàng)

方法

編譯

re.compile(pattern,flags =0)
設(shè)定flags,編譯模式,返回正則表達(dá)式對(duì)象regex。
pattern就是正則表達(dá)式字符串,flags是選項(xiàng)。正則表達(dá)式需要被編譯,為了提高效率,這些被編譯后的結(jié)果被保存,下次使用同樣的pattern的時(shí)候,就不需要再次編譯。
re的其他方法為了提高效率都調(diào)用了編譯方法,就是為了提速

單次匹配

re.match(pattern,string,flags=0)
regex.match(string[,pos[,endpos]])
match匹配從字符串的開(kāi)頭匹配,regex對(duì)象match方法可以重設(shè)定開(kāi)始的位置和結(jié)束位置,返回match對(duì)象
re.search(pattern,string,flags=0)
regex.search(string[,pos[,endpos]])
從頭搜索直到第一個(gè)匹配,regex對(duì)象search方法可以重新設(shè)定開(kāi)始和結(jié)束位置,返回match對(duì)象
re.fullmatch(pattern,string,flags=0)
regex.fullmatc(string[,pos[,endpos]])
整個(gè)字符串和正則表達(dá)式匹配

import re 
s = '''bottle\nbag\nbig\napple'''
for i,c in enumerate(s,1):
        print((i-1,c),end = '\n' if i%8==0 else ' ')
print()

(0, 'b') (1, 'o') (2, 't') (3, 't') (4, 'l') (5, 'e') (6, '\n') (7, 'b')
(8, 'a') (9, 'g') (10, '\n') (11, 'b') (12, 'i') (13, 'g') (14, '\n') (15, 'a')
(16, 'p') (17, 'p') (18, 'l') (19, 'e') 
#match 方法
print('--match--')
result = re.match('b',s) # 找到一個(gè)就不找了
print(1,result)
result = re.match('a',s)
print(2,result) #沒(méi)找到返回None
result = re.match('^a',s,re.M)# 依然從頭開(kāi)始找,多行模式?jīng)]有用
print(3,result)
result = re.match('^a',s,re.S)#依然從頭開(kāi)始找
print(4,result)
#先編譯,再使用正則表達(dá)式表達(dá)
regex = re.compile('a')
result = regex.match(s) #依然從頭開(kāi)始找
print(5,result)
result = regex.match(s,15)  # 把索引15作為開(kāi)始找
print(6,result)
print()

search方法
print('--search--')
result = re.search('a',s) # 掃描找到匹配的第一個(gè)位置
print(7,result)
regex = re.compile('b')
result = regex.search(s,1)
print(8,result) # bag
regex = re.compile('^b',re.M)
result = regex.search(s) # 不管是不是多行,找到就返回
print(8.5,result) #bootle
result = regex.search(s,8)
print(9,result) #big

fullmatch方法
result = re.fullmatch('bag',s)
print(10,result)
regex = re.compile('bag')
result = regex.fullmatch(s)
print(11,result)
result = regex.fullmatch(s,7)
print(12,result)
result = regex.fullmatch(s,7,10)
print(13,result) # 要完全匹配,多了少了都不行,[7,10)
全文方法
re.findall(pattern,string,flags=0)
regex.findall(string[,pos[,endpos]])
對(duì)整個(gè)字符串,從左至右匹配,返回所有匹配項(xiàng)的列表
re.finditer(pattern,string,flags=0)
regex.fingiter(string[,pos[,endpos]])
對(duì)整個(gè)字符串,從左至右匹配,返回所有匹配項(xiàng),返回迭代器
注意每次迭代返回的是match對(duì)象。
import re 
s = '''bottle\nbag\nbig\nable'''
for i,c in enumerate(s,1):
        print((i-1,c),end = '\n' if i%8==0 else ' ')
print()

(0, 'b') (1, 'o') (2, 't') (3, 't') (4, 'l') (5, 'e') (6, '\n') (7, 'b')
(8, 'a') (9, 'g') (10, '\n') (11, 'b') (12, 'i') (13, 'g') (14, '\n') (15, 'a')
(16, 'b') (17, 'l') (18, 'e') 
findall 方法
result = re.findall('b,',s)
print(1,result)
regex = re.compile('^b')
result = regex.findall(s)
print(2,result)
regex = re.compile('^b',re.M)
result = regex.findall(s,7)
print(3,result) #bag big
regex = re.compile('^b',re.S)
result = regex.findall(s)
print(4,result) # bottle
regex = re.compile('^b',re.M)
result = regex.findall(s,7,10)
print(5,result)  # bag
fiditer 方法
result = regex.finditer(s)
print(type(result))
print(next(result))
print(next(result))

匹配替換

re.sub(pattern,replacement,string,count=0,flags=0)
regex.sub(replacement,string,count=0)
使用pattern對(duì)字符串string進(jìn)行匹配,對(duì)匹配項(xiàng)使用repl替換。
replacement可以是string、bytes、function
re.subn(pattern,replace,string,count=0,flags=0)
regex.subn(replance,string,string,count=0)
同sub返回一個(gè)元組(new_string,number_of_subs_made)
import re 
s = '''bottle\nbag\nbig\napple'''
for i,c in enumerate(s,1):
        print((i-1,c),end = '\n' if i%8==0 else ' ')
print()

(0, 'b') (1, 'o') (2, 't') (3, 't') (4, 'l') (5, 'e') (6, '\n') (7, 'b')
(8, 'a') (9, 'g') (10, '\n') (11, 'b') (12, 'i') (13, 'g') (14, '\n') (15, 'a')
(16, 'p') (17, 'p') (18, 'l') (19, 'e') 

替換方法
regex = re.compile('b\wg')
result = regex.sub('magedu',s)
print(1,result)
result = regex.sub('magedu',s,1)
print(2,result)

regex = re.compile('\s+')
result = regex.subn('\t',s)
print(3,result)  # 被替換后的字符串及替換次數(shù)的元組
分割字符串
re.split(pattren,string,maxsplit,flags=0)
re.split(分割字符串)
import re
s = '''01 bottle
02 bag
03    big1
100    able'''
for i,c in enumerate(s,1):
        print((i-1,c),end='\n' if i%8==0 else ' ')
print()
把每行單詞提取出來(lái)
print(s.split())  #做不到['01'...'big1'...]
result = re.split('[\s\d]+',s)
print(1,result)
regex = re.compile('^[\s\d]+')
result = regex.split(s)
print(2,result)
regex = re.compile('^[\s\d]+',re.M)
result = regex.split(s)
print(3,result)
regex = re.compile('\s\d+\s+')
result = regex.split(' '+s)
print(4,result)

分組

使用小括號(hào)的pattern捕獲的數(shù)據(jù)被放到了組group中。
match、search函數(shù)可以返回match對(duì)象,findall返回字符串列表,finditer返回一個(gè)match對(duì)象
如果pattern中使用了分組,如果匹配有結(jié)果,會(huì)在match對(duì)象中
1、使用group(N)方式返回對(duì)應(yīng)分組,1到N是對(duì)應(yīng)分組,0返回整個(gè)匹配字符
2、如果使用了命名分組,可以使用group('name')的方式取分組
3、使用groupdict()返回所有命名的分組
import re 
s = '''bottle\nbag\nbig\napple'''
for i,c in enumerate(s,1):
        print((i-1,c),end = '\n' if i%8==0 else ' ')
print()

#分組
regex = re.compile('(b\w+)')
result = regex.match(s)
print(type(result))
print(1,'match',result.groups())
result = regex.search(s,8)
print(2,'match',result.groups())
#命名分組
regex = re.compile('(b\w+)\n(?P<name2>b\w+)\n(?P<name3>b\w+)')
result = regex.match(s)
print(3,'match',result)
print(4,result.group(3),result.group(2),result.group(1))
print(5,result.group(0).encode()) # 0返回整個(gè)匹配字符串,即match
print(6,result.group('name2'),result.group('name3'))
print(7,result.groups())
print(8,result.groupdict())

result = regex.findall(s)
for x in result:  # 字符串列表
        print(type(x),x)

regex = re.compile('(?P<head>b\w+)')
result = regex.finditer(s)
for x in result:
        print(type(x),x,x.group(),x.group('head'))

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。

網(wǎng)站標(biāo)題:python正則表達(dá)式-創(chuàng)新互聯(lián)
鏈接分享:http://www.muchs.cn/article32/dhjdpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開(kāi)發(fā)、企業(yè)建站、外貿(mào)網(wǎng)站建設(shè)App設(shè)計(jì)、網(wǎng)站排名、定制開(kāi)發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

綿陽(yáng)服務(wù)器托管