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

 

相关文章:

  • 2021-12-27
  • 2022-12-23
  • 2021-12-10
  • 2021-07-17
  • 2022-03-07
  • 2022-12-23
  • 1970-01-01
猜你喜欢
  • 2022-01-21
  • 2022-12-23
  • 2021-07-24
  • 2022-12-23
  • 2022-01-19
  • 2021-05-18
  • 2022-12-23
相关资源
相似解决方案