简体   繁体   中英

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)      
>>> buffer="tags = { 'one' : \"two\", \"three\", 'four' }"
>>> re.findall(r"['\"](.*?)['\"]", buffer)
['one', 'two', 'three', 'four']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM