简体   繁体   English

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

[英]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. 我需要Python正则表达式的帮助来提取单引号或双引号内的字符串。 I found a solution but the regex is in C# here: 我找到了一个解决方案,但正则表达式在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)      
>>> buffer="tags = { 'one' : \"two\", \"three\", 'four' }"
>>> re.findall(r"['\"](.*?)['\"]", buffer)
['one', 'two', 'three', 'four']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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