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'
#这种格式的才会有不止一个元组

#运行结果
#[('i', 'love', 'you')]

print(c[0][2])
#运行结果
#you

猜你喜欢

转载自www.cnblogs.com/chillytao-suiyuan/p/9073627.html