Return any number of matching groups with re findall in python
<p>I have a relatively complex string that contains a bunch of data. I am trying to extract the relevant pieces of the string using a regex command. The portions I am interested in are contained in square brackets, like this:</p>

<pre><code>s = '"data":["value":3.44}] lol haha "data":["value":55.34}] 
                "data":["value":2.44}] lol haha "data":["value":56.34}]'
</code></pre>

<p>And the regex expression I have built is as follows:</p>

<pre><code>l = re.findall(r'\"data\"\:.*(\[.*\])', s)
</code></pre>

<p>I was expecting this to return</p>

<pre><code>['["value":3.44}]', '["value":55.34}]', '["value":2.44}]', '["value":56.34}]']
</code></pre>

<p>But instead all I get is the last one, i.e., </p>

<pre><code>['["value":56.34}]']
</code></pre>

<p>How can I catch 'em all? </p>

<p>It's because quantifiers are greedy by default. So <code>.*</code> will match everything between the first <code>"data":</code> and the last <code>[</code>, so there's only one <code>[...]</code> left to match.</p>

<p>Use non-greedy quantifiers by adding <code>?</code>.</p>

<pre><code>l = re.findall(r'\"data\"\:.*?(\[.*?\])', s)
</code></pre>

python regex