FranGoitia
FranGoitia

Reputation: 1993

Capturing string in Python

So, I'm trying to capture this big string in Python but it is failing me. The regex I wrote works fine in regexr: http://regexr.com/3cmdc

But trying to using it in Python to capture the text returns None. This is the code:

pattern = "var initialData = (.*?);\\n"
match = re.search(pattern, source).group(1)

What am I missing ?

Upvotes: 0

Views: 293

Answers (2)

Jannik Becher
Jannik Becher

Reputation: 189

Use pythons raw string notation:

pattern = r"var initialData = (.*?);\\n"
match = re.search(pattern, source).group(1)

More information

Upvotes: 1

alecxe
alecxe

Reputation: 473813

You need to set the appropriate flags:

re.search(pattern, source, re.MULTILINE | re.DOTALL).group(1)

Upvotes: 1

Related Questions