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>You can also use <a href="https://docs.python.org/2/library/re.html#re.finditer" rel="nofollow"><code>finditer</code></a> to extract the relevant content iteratively:</p>

<pre><code>import re

s = '"data":["value":3.44}] lol haha "data":["value":55.34}] "data":["value":2.44}] lol haha "data":["value":56.34}]'
for m in re.finditer(r'(\[.*?\])', s):
    print m.group(1)
</code></pre>

<p><strong>OUTPUT</strong></p>

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

python regex