How to get the first occurrence ? regex python -
i have html tag:
x=""" <div>ad</div> \n\n <div> correct value </div> <div> wrong value </div> """
i want corret value
so search word ad
followed </div>
thing until <div>
values until </div>
i use code:
re.findall(r'ad</div>.*<div>(.*)</div>',x,re.s)
i use falg re.s because want dot match new line also. don't know how lines there between divs. use .* !
i think findall should return correct value
, return wrong value
. why ? search last div not first 1 ?
because have greedy
try lazy :
re.findall(r'ad</div>.*?<div>(.*?)</div>',x,re.s)
in example .*
matching towards end , sees <div>
, regex tracks , and startes matching again, similar second scenario,
demo here :
Comments
Post a Comment