import re
key=r"<h1>Hello World</h1><h2>Hello error</h3><h2>Hello everyone</h2><h3>Hello everybody</h3><h4>Hello ladies</h4>"
p1=r"(?<=<(h[1-4]>)).+?(?=</\1>)"
pattern1=re.compile(p1)
objs=pattern1.finditer(key)
list=[]
for obj in objs:
list.append(obj.group())
print (list)
运行结果为[ ]
括号打错位置了,p1=r"(?<=<(h[1-4]>)).+?(?=</\1>)"改为p1=r"(?<=<(h[1-4])>).+?(?=</\1>)"