Python版本為3.4.3
直接看官方文檔。。。記錄當筆記
https://docs.python.org/3/library/re.html?highlight=re#module-re
和一般的正則表達式不同,Python用想表示一個'/'必須用'//',而正則表達式中是用'//'表示一個反斜杠的,所以在Python中要用'////'表示一個'/',太麻煩,所以可以在字符串前面加上'r'表示這里面的字符都是不需要轉義的原生態字符。
現在我們所要分析的字符串為"fsfxczf//sadfDOTA2s////dafasdsfDOTA2wwwwwwwwwww"。
re提供了兩種簡單的匹配方式(查找字符串是否有某個字符串),一種是search,另一種是match(匹配開頭)。
import regame = r'doTa2fsfxczf//sadfDOTA2s////dafasdsfDOTA2wwwwwdotawwwwww'#三個參數分別是,簡單的正則表達式,所匹配的字符串,匹配模式(可以用數字代替)game2=re.match('dota2',game,re.I)#匹配模式為忽略大小寫PRint(game2)#<_sre.SRE_Match object; span=(0, 5), match='doTa2'>
這個模塊中的方法如下:
import regame = r'doTa2fsfxczf//sadfDOTA2s////dafasdsfDOTA2wwwwwdotawwwwww'style='DOTA.'rengine = re.compile(style,re.I)gamename= rengine.findall(game)print(gamename)#['doTa2', 'DOTA2', 'DOTA2', 'dotaw']
| re.A/re.ASCII | 值為256,僅匹配ASCII碼值 |
| re.I/re.IGNORECASE | 值為2,無視大小寫 |
| re.DEBUG | 值為128,顯示編譯表達式的debug信息 |
| re.L/re.LOCALE | 值為4,暫時沒有理解是什么意思 |
| re.M/re.MULTILINE | 值為8,使得匹配可以跨行 |
temp=r'this is a temp'print(re.fullmatch(temp,'this is a temp'))print(re.fullmatch(temp,'this is b temp'))#<_sre.SRE_Match object; span=(0, 14), match='this is a temp'>#None
temp = r'this is DOTA2!'print(re.split(' ', temp, 0, re.I))print(re.split(' ', temp, 1, re.I))#['this', 'is', 'DOTA2!']#['this', 'is DOTA2!']
temp =r'this is dota,and this is DOTA2.'print(re.findall('dota.',temp,re.I))#['dota,', 'DOTA2']
temp =r'this is dota,and this is DOTA2.'ite = re.finditer('dota.',temp,re.I)for obj in ite: print(obj.group())#dota,#DOTA2
temp = r'this-is-dota!'print(re.sub('6*', ' ', temp)) # *present zero or more# t h i s - i s - d o t a !def show(repl): # there must be a parameter print('play') return '+'print(re.sub('-', show, temp))# play# play# this+is+dota!
temp = r'this-is-dota!'print(re.subn('-','*',temp))#tuple#('this*is*dota!', 2)
temp = r'<script>'print(re.escape(temp))# /<script/>
之后就是經過re.compile()之后的regex對象了
變成regex對象之后的方法和直接使用re加載正則匹配模式的方法大體上差不多,只不過多了以下幾種方法:
具體如下:
temp ='this is dota,that is dota2,this is war3!'r0=re.compile('(dota)(.)(?P<nickname>....)',re.I)print(r0.flags)print(r0.groups)print(r0.groupindex)print(r0.pattern)print(r0.search(temp).group('nickname'))print(r0.search(temp).group(2))# 34# 3# {'nickname': 3}# (dota)(.)(?P<nickname>....)# that# ,
最后就是match對象了
temp ='this is dota,that is dota2,this is war3!'r0=re.compile('(....)(.)(..)')matchobj=r0.search(temp)print(matchobj)print(matchobj.expand(r'/3/2/1'))# <_sre.SRE_Match object; span=(0, 7), match='this is'># is this
temp ='this*is dota,that is dota2,this is war3!'r0=re.compile('(....)(?P<second>.)(..)')tem=r0.search(temp)print(tem.group(0))#the entire matchprint(tem.group(1))print(tem.group(2))print(tem.group('second'))print(tem.group(3))print(tem.group(1,2))# a tuple# this*is# this# *# *# is# ('this', '*'
temp ='this is dota,that is dota2,this is war3!'m=re.match('(....)(.)(..)',temp)print(m.groups())print(type(m.groups()))# ('this', ' ', 'is')# <class 'tuple'>
temp ='this is dota,that is dota2,this is war3!'
m=re.match('(....)(?P<Word1>.)(?P<word2>..)',temp)
print(m.groupdict())#返回所有有別名的組
# {'word1': ' ', 'word2': 'is'}temp ='this is dota,that is dota2,this is war3!'reg =re.compile('dota')m=reg.search(temp,10,30)print(m.pos)#開始匹配的索引print(m.endpos)#返回所匹配字符串的最后一位的索引# 10# 30
temp ='this is dota,that is dota2,this is war3!'m=re.match('(....)(?P<Tom>.)(?P<Mary>..)',temp)print(m.lastindex)#返回最后一個匹配組的索引,如果壓根沒有匹配成功的,為Noneprint(m.lastgroup)#返回最后一個匹配組的名字,如果沒有,則返回Noneprint(m.re)#返回該正則print(m.string)#返回匹配字符串# 3# Mary# re.compile('(....)(?P<Tom>.)(?P<Mary>..)')# this is dota,that is dota2,this is war3!
temp ='this is dota,that is dota2,this is war3!'m=re.match('(....)(?P<word1>.)(?P<word2>..)',temp)print(m.start(1))print(m.end(2))print(m.span(2))# 0# 5# (4, 5)
以上
新聞熱點
疑難解答