国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Python3re模塊

2019-11-14 17:06:09
字體:
來源:轉載
供稿:網友

  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'>

   這個模塊中的方法如下:

  • re.compile(pattern,flags):這個方法是對一個正則表達式變成了一個表達式對象,可以多次使用。
import regame = r'doTa2fsfxczf//sadfDOTA2s////dafasdsfDOTA2wwwwwdotawwwwww'style='DOTA.'rengine = re.compile(style,re.I)gamename= rengine.findall(game)print(gamename)#['doTa2', 'DOTA2', 'DOTA2', 'dotaw']
  • 其中常見的flag值可以為如下幾種:
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,使得匹配可以跨行
  • re.fullmatch(pattern,flag):完全相同則返回match對象,否則none
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
  • re.split(pattern,string,splitpart,flag):以pattern分割字符串,最多可以分割splitpart個:
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!']
  • re.findall(pattern,string,flag):找到符合pattern的所有字符串,并返回列表:
temp =r'this is dota,and this is DOTA2.'print(re.findall('dota.',temp,re.I))#['dota,', 'DOTA2']
  • re.finditer(pattern,string,flag):返回一個迭代器:
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
  • re.sub(pattern,replace||function,string,flag):用字符串或者函數返回值代替符合pattern的字符串
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!
  • re.subn():這個函數和上面那個效果一樣,只不過返回的是一個tuple。
temp = r'this-is-dota!'print(re.subn('-','*',temp))#tuple#('this*is*dota!', 2)
  • re.escape(string):轉義除了數字,字母的其他ASCII字符。
temp = r'<script>'print(re.escape(temp))# /<script/>
  • re.purge():釋放正則表達式緩存。。。然后我現在并沒有發現什么時候會產生正則表達式緩存。。。

   之后就是經過re.compile()之后的regex對象了

  變成regex對象之后的方法和直接使用re加載正則匹配模式的方法大體上差不多,只不過多了以下幾種方法:

  • regex.flags  #得到該正則對象的flags值
  • regex.pattern#得到該正則對象的樣式
  • regex.groups#得到該正則對象的分組
  • regex.groupindex#得到該正則表達式對象有別名的值

  具體如下:

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對象了

  • match.expand():將得到的字符按照相應的模板處理
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
  • match.group():找匹配結果的分組
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', '*'
  • match.groups():如果匹配成功得到一個tuple結果
  • match.groupdict():返回所有有別名的匹配組結果,結果是一個dict
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'}
  • match.endpos():
  • match.pos():
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
  • match.lastindex
  • match.lastgroup
  • match.re
  • match.string
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!
  • match.start(group):
  • match.end(group):得到某個匹配組開始/結束匹配的索引值
  • match.span(group):得到某個匹配組的跨度。。。
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)

  以上

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 聂荣县| 南部县| 龙川县| 扶风县| 黄陵县| 临邑县| 泊头市| 剑川县| 中山市| 唐海县| 五寨县| 通河县| 图们市| 鄄城县| 襄城县| 天台县| 巩义市| 桃园市| 通江县| 陆川县| 牙克石市| 奎屯市| 拉萨市| 积石山| 界首市| 达州市| 明水县| 双流县| 雅安市| 连平县| 开封市| 利津县| 三门峡市| 武山县| 伊通| 祁连县| 昌邑市| 封开县| 沾益县| 永昌县| 北碚区|