user3606874 user3606874 - 1 month ago 7x
Python Question

Return any number of matching groups with re findall in python

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:

s = '"data":["value":3.44}] lol haha "data":["value":55.34}]
"data":["value":2.44}] lol haha "data":["value":56.34}]'

And the regex expression I have built is as follows:

l = re.findall(r'\"data\"\:.*(\[.*\])', s)

I was expecting this to return

['["value":3.44}]', '["value":55.34}]', '["value":2.44}]', '["value":56.34}]']

But instead all I get is the last one, i.e.,


How can I catch 'em all?


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

Use non-greedy quantifiers by adding ?.

l = re.findall(r'\"data\"\:.*?(\[.*?\])', s)