Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extracting strings in Python in either single or double quotes

I need help with a Python regex to extract strings either inside single or double quoted. I found a solution but the regex is in C# here:

How to extract the string in the quotes (either double quotes or single quotes)

I need to parse this string

tags = { 'one' : "two", "three", 'four' }

and return the array items:

one
two
three
four

Currently I have this for single quotes:

quoted = re.findall(r"'(.*?)'", buffer, re.DOTALL)      
like image 989
Ron Avatar asked Oct 30 '13 06:10

Ron


1 Answers

>>> buffer="tags = { 'one' : \"two\", \"three\", 'four' }"
>>> re.findall(r"['\"](.*?)['\"]", buffer)
['one', 'two', 'three', 'four']
like image 134
devnull Avatar answered Nov 14 '22 23:11

devnull