python正则表达查找问题

贪心和非贪心查找
import re
numreg1 = re.compile(r'(ha){2,4}ll')
numreg2 = re.compile(r'(ha){2,4}?ll') #***为何非贪心查找不生效?***
numreg22 = re.compile(r'ttt(ha){2,4}?')
numreg3 = re.compile(r'(ha){2,4}')
numreg4 = re.compile(r'(ha){2,4}?')
s3 = 'ttthahahahahahahally'
mo1 = numreg1 .search(s3)
mo2 = numreg2 .search(s3)
mo22 = numreg22 .search(s3)
mo3 = numreg3 .search(s3)
mo4 = numreg4 .search(s3)
print(mo1.group())
print(mo2.group())
print(mo22.group())
print(mo3.group())
print(mo4.group())
输出:
hahahahall
hahahahall
ttthaha
hahahaha
haha

贪婪匹配存在方向性,它从左到右尽量少匹配,但是不能把已经匹配的“吐”出来。
从左往右,找到hahahaha然后才找到ll
你可以用

 import re

numreg2 = re.compile(r'.*(ha)(?=(ha){2,4}?ll)')
s3 = 'ttthahahahahahahally'
mo2 = numreg2.search(s3)
s4 = mo2.group()
s4 = s3.replace(s4, "")
print(s4)