python正則表達式

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

常量

常量                                     說明
re.M、re.MULTILINE          多行模式  
re.S、re.DOTALL                單行模式
re.I、re.IGNORECASE       忽略大小寫
re.X、re.VERBOSE            忽略表達式中的空白字符
使用 | 位或 運算開啓多種選項

方法

編譯

re.compile(pattern,flags =0)
設定flags,編譯模式,返回正則表達式對象regex。
pattern就是正則表達式字符串,flags是選項。正則表達式需要被編譯,爲了提高效率,這些被編譯後的結果被保存,下次使用同樣的pattern的時候,就不需要再次編譯。
re的其他方法爲了提高效率都調用了編譯方法,就是爲了提速

單次匹配

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

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) # 找到一個就不找了
print(1,result)
result = re.match('a',s)
print(2,result) #沒找到返回None
result = re.match('^a',s,re.M)# 依然從頭開始找,多行模式沒有用
print(3,result)
result = re.match('^a',s,re.S)#依然從頭開始找
print(4,result)
#先編譯,再使用正則表達式表達
regex = re.compile('a')
result = regex.match(s) #依然從頭開始找
print(5,result)
result = regex.match(s,15)  # 把索引15作爲開始找
print(6,result)
print()

search方法
print('--search--')
result = re.search('a',s) # 掃描找到匹配的第一個位置
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]])
對整個字符串,從左至右匹配,返回所有匹配項的列表
re.finditer(pattern,string,flags=0)
regex.fingiter(string[,pos[,endpos]])
對整個字符串,從左至右匹配,返回所有匹配項,返回迭代器
注意每次迭代返回的是match對象。
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對字符串string進行匹配,對匹配項使用repl替換。
replacement可以是string、bytes、function
re.subn(pattern,replace,string,count=0,flags=0)
regex.subn(replance,string,string,count=0)
同sub返回一個元組(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)  # 被替換後的字符串及替換次數的元組
分割字符串
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()
把每行單詞提取出來
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)

分組

使用小括號的pattern捕獲的數據被放到了組group中。
match、search函數可以返回match對象,findall返回字符串列表,finditer返回一個match對象
如果pattern中使用了分組,如果匹配有結果,會在match對象中
1、使用group(N)方式返回對應分組,1到N是對應分組,0返回整個匹配字符
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返回整個匹配字符串,即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'))
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章