0

I have a string:

s = r'"url" : "a", "meta": "b", "url" : "c"'

What I want is to capture the substring url: ... up to the ,, so the expected output is a list:

[r'"url" : "a"', r'"url" : "b"']

I am using:

re.findall(r'("url"):(.*),', s)

but all it does is to return the entire string. Is there something i am doing wrong?

0

2 Answers 2

3

Your last "," was beeing matched due to a greedy search, (.*?) is non greedy. Also the last comma is optional so that needs to be ignored if not present

import re

s = r'"url":"a","meta":"b","url":"c"'

print(re.findall(r'("url"):"(.*?)",?', s))
Sign up to request clarification or add additional context in comments.

Comments

1

You must escape the , to avoid including the comma inside the group. Try this:

re.findall(r'(("url" :[^,]*),*)', s)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.