python findall finds only the last occurrence
python, regex
Solution
The `.*` at the beginning is very greedy and it consumes till the last occurrence. In fact all the `.*` in the expression are very greedy. So, we make them non-greedy with `?`, like this
p = re.compile(r'.*?<section\s*id="(.+?)">(.+?)</section>.*?')
And the output becomes
id=[1], text=[2]
id=[3], text=[4]
In fact, you can drop the first and last `.*` patterns and keep it simple like this
p = re.compile(r'<section\s*id="(.+?)">(.+?)</section>')
Problem
I'm trying to extract some data from webpage using Python 2.7.5. code: ``` p = re.compile(r'.*<section\s*id="(.+)">(.+)</section>.*') str = 'df <section id="1">2</section> fdd <section id="3">4</section> fd' m = p.findall(str) for eachentry in m: print 'id=[{}], text=[{}]'.format(eachentry[0], eachentry[1]) ``` output: ``` id=[3], text=[4] ``` why it's extracting only the last occurrence? if i remove the last occurrence the first one is found