Discuss / Python / 老师,碰到一个很奇怪的问题

老师,碰到一个很奇怪的问题

Topic source

wsc晚熟超

#1 Created at ... [Delete] [Delete and Lock User]
html = 'xxx<html>xxxx'
hosPattern = re.compile('<html>')
hosName = hosPattern.match(html)
print(hosName)

这样匹配不到,输出None

html = 'xxx<html>xxxx'
hosPattern = re.compile('<html>')
hosName = re.findall(hosPattern,html)
print(hosName)

这样就能匹配到,输出['<html>']

搞了一下午还是没搞明白

大彭1111

#2 Created at ... [Delete] [Delete and Lock User]
>>> html = 'xxx<html>xxxx'
>>> hosPattern = re.compile(r'.*<html>.*')
>>> hosName = hosPattern.match(html)
>>> print hosName
<_sre.SRE_Match object at 0x4b60c8>

这样子才是你的意思吧。

findall是寻找匹配的子串,所以你哪个才可以匹配


  • 1

Reply