elrich bachman
elrich bachman

Reputation: 176

how to extract values inside quote using regex python 3.x

I have following

\"url\": \"\\\/maru.php?superalox=1\", \"params\": {\"params\": \"EgtmbUtXdUZWdDF2SSoCCABQAQ%3D%3D\", \"session_value\": \"QUFFLUhqbnQ4eW5HeEZYdDBULU5EVk1LREU2VndMMm1nd3xBQ3Jtc0trMUlMOWRqTWxpS0pOT2pNUVN6RENVU3k0Tmc4blplodexsWkxrVDRmOUN2Q0lXVkl1N0YwUFhoV1puQ3ZFQm10X1RzNWR4Q3RUeG5kMkdLNnNobTUyRkNuaG90d2c=\"}, \"log_params\":

i want to extract the value of params which is EgtmbUtXdUZWdDF2SSoCCABQAQ%3D%3D

i have tried this but it didnt work

my_text = """ \"url\": \"\\\/maru.php?superalox=1\", \"params\": {\"params\": \"EgtmbUtXdUZWdDF2SSoCCABQAQ%3D%3D\", \"session_value\": \"QUFFLUhqbnQ4eW5HeEZYdDBULU5EVk1LREU2VndMMm1nd3xBQ3Jtc0trMUlMOWRqTWxpS0pOT2pNUVN6RENVU3k0Tmc4blplodexsWkxrVDRmOUN2Q0lXVkl1N0YwUFhoV1puQ3ZFQm10X1RzNWR4Q3RUeG5kMkdLNnNobTUyRkNuaG90d2c=\"}, \"log_params\": """

extract_data = re.search(r'(\\\"params\": \\\")(\w*)', my_text)
print(extract_data)

Thanks

Upvotes: 0

Views: 67

Answers (2)

llllllllll
llllllllll

Reputation: 16404

You can use:

re.search(r'"params": "([^"]+)"', my_text).group(1)

Upvotes: 1

Mark Hollis
Mark Hollis

Reputation: 151

I've been using the following with great success:

(["'])(?:(?=(\\?))\2.)*?\1

It supports nested quotes as well.

This also works: (["'])(\?.)*?\1 Easier to read.

For those who want a deeper explanation of how this works, here's an explanation from user ephemient:

([""']) match a quote; ((?=(\\?))\2.) if backslash exists, gobble it, and whether or not that happens, match a character; *? match many times (non-greedily, as to not eat the closing quote); \1 match the same quote that was use for opening.

This is copied from another answer found here: RegEx: Grabbing values between quotation marks

Upvotes: 0

Related Questions