pythonRegex

來源:互聯網
上載者:User

標籤:enumerate   字串和Regex   單詞   括弧   元組   運算   設定   單行   imp   

python使用re模組提供了Regex的處理能力

常量

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

編譯

re.compile(pattern,flags =0)設定flags,編譯模式,返回Regex對象regex。pattern就是Regex字串,flags是選項。Regex需要被編譯,為了提高效率,這些被編譯後的結果被儲存,下次使用同樣的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]])整個字串和Regex匹配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) #沒找到返回Noneresult = re.match(‘^a‘,s,re.M)# 依然從頭開始找,多行模式沒有用print(3,result)result = re.match(‘^a‘,s,re.S)#依然從頭開始找print(4,result)#先編譯,再使用Regex表達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) # bagregex = re.compile(‘^b‘,re.M)result = regex.search(s) # 不管是不是多行,找到就返回print(8.5,result) #bootleresult = regex.search(s,8)print(9,result) #bigfullmatch方法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 bigregex = re.compile(‘^b‘,re.S)result = regex.findall(s)print(4,result) # bottleregex = re.compile(‘^b‘,re.M)result = regex.findall(s,7,10)print(5,result)  # bagfiditer 方法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、functionre.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 res = ‘‘‘01 bottle02 bag03    big1100    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返回整個匹配字串,即matchprint(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‘))

pythonRegex

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.