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 :

http://regex101.com/r/zy9xa3/1


Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -