用单引号或双引号在Python中提取字符串

时间:2022-09-15 13:32:57

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:

我需要Python正则表达式的帮助来提取单引号或双引号内的字符串。我找到了一个解决方案,但正则表达式在C#中:

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)      

1 个解决方案

#1


8  

>>> buffer="tags = { 'one' : \"two\", \"three\", 'four' }"
>>> re.findall(r"['\"](.*?)['\"]", buffer)
['one', 'two', 'three', 'four']

#1


8  

>>> buffer="tags = { 'one' : \"two\", \"three\", 'four' }"
>>> re.findall(r"['\"](.*?)['\"]", buffer)
['one', 'two', 'three', 'four']