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

时间:2013-10-30 06:58:19

标签: python regex string quotes findall

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

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

我需要解析这个字符串

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

并返回数组项:

one
two
three
four

目前我有单引号:

quoted = re.findall(r"'(.*?)'", buffer, re.DOTALL)      

1 个答案:

答案 0 :(得分:8)

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