Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Multiline Regular Expressions in Python?

I am using regular expressions in Python to search through a page source, and find all the json information in the javascript. Specifically an example would look something like this:

var fooData = {
    id: 123456789,
    name : "foo bar",
    country_name: "foo",
    country_is_eu: null,
    foo_bars: null,
    foo_email: null,
    foo_rate: 1.0,
    foo_id: 0987654321
};

I'm fairly new to understanding all there is to know about regular expressions, and I'm not sure if what I'm doing is correct. I can get some individual lines, but I'm not completely sure of how to use re.MULTILINE. This is the code I have so right now:

prog = re.compile('[var ]?\w+ ?= ?{[^.*]+\n};', re.MULTILINE)
vars = prog.findall(text)

Why is this not working?

To be more clear, I really need it to match everything in between these brackets like this:

var fooData = {

};

So, essentially I can't figure out a way to match every line except one that looks like this:

};
like image 245
jackcogdill Avatar asked Feb 05 '26 03:02

jackcogdill


1 Answers

This is what you are looking for not including the brackets:

(?<=var fooData = {)[^}]+(?=};)

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!