python正則表達式02--findall()和search()方法區別,group()方法


import re

st = 'asxxixxsaefxxlovexxsdwdxxyouxxde'

#search()和 findall()的區別

a = re.search('xx(.*?)xxsaefxx(.*?)xxsdwdxx(.*?)xx',st)
#print(a)
#運行結果
#<_sre.SRE_Match object; span=(2, 30), match='xxixxsaefxxlovexxsdwdxxyouxx'>

#group()方法
b = re.search('xx(.*?)xxsaefxx(.*?)xxsdwdxx(.*?)xx',st).group(3)
print(b)
#運行結果
#you

c = re.findall('xx(.*?)xxsaefxx(.*?)xxsdwdxx(.*?)xx',st)
print(c)

#結果中列表中包含一個元組,只有待匹配字符串中還有
#'xx(.*?)xxsaefxx(.*?)xxsdwdxx(.*?)xx'
#例如:
xx(.*?)xxsaefxx(.*?)xxsdwdxx(.*?)xx+++xx(.*?)xxsaefxx(.*?)xxsdwdxx(.*?)xx
#這種格式的才會有不止一個元組

#運行結果
#[('i', 'love', 'you')]

print(c[0][2])
#運行結果
#you

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM