问答详情
源自:4-3 回溯引用

运行结果与课程不一致

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)

运行结果为[ ]

提问者:wo蜗牛niu 2020-02-24 11:37

个回答

  • wo蜗牛niu
    2020-02-24 12:27:54

    括号打错位置了,p1=r"(?<=<(h[1-4]>)).+?(?=</\1>)"改为p1=r"(?<=<(h[1-4])>).+?(?=</\1>)"