我有这个json文件。部分内容如下:
{ "has_more" : false,
"items" : [ { "aliases" : [ "http://www.stackoverflow.com" ],
"api_site_parameter" : "stackoverflow",
"markdown_extensions" : [ "Prettify" ],
"name" : "Stack Overflow",
"related_sites" : [ { "name" : "Stack Overflow Chat",
"relation" : "chat",
"site_url" : "http://chat.stackoverflow.com"
} ],
"site_state" : "normal",
"site_type" : "main_site",
"site_url" : "http://stackoverflow.com",
"styling" : { "link_color" : "#0077CC",
"tag_background_color" : "#E0EAF1",
"tag_foreground_color" : "#3E6D8E"
}
},
{ "api_site_parameter" : "serverfault",
"markdown_extensions" : [ "Prettify" ],
"name" : "Server Fault",
"related_sites" : [ { "api_site_parameter" : "meta.serverfault",
"name" : "Meta Server Fault",
"relation" : "meta",
"site_url" : "http://meta.serverfault.com"
},
{ "name" : "Chat Stack Exchange",
"relation" : "chat",
"site_url" : "http://chat.stackexchange.com"
}
],
"site_state" : "normal",
"site_type" : "main_site",
"site_url" : "http://serverfault.com",
"styling" : { "link_color" : "#10456A",
我想匹配像
这样的字符串 "related_sites" : [ { "name" : "Stack Overflow Chat",
"relation" : "chat",
"site_url" : "http://chat.stackoverflow.com"
} ],
和
"related_sites" : [ { "api_site_parameter" : "meta.serverfault",
"name" : "Meta Server Fault",
"relation" : "meta",
"site_url" : "http://meta.serverfault.com"
},
{ "name" : "Chat Stack Exchange",
"relation" : "chat",
"site_url" : "http://chat.stackexchange.com"
}
],
没有启用多行。知道怎么做吗?
答案 0 :(得分:1)
“启用多行”正是您匹配多行的方式。这就是为什么它被称为“多线”。但由于它所做的只是更改.
以包含\n
,您可以改为编写(.|\n)
。
但鉴于这是JSON,为什么你在使用正则表达式呢?只需将其解析为数据结构并从那里开始工作。
data = json.loads(json_string)
for item in data['items']:
print item['related_sites']
答案 1 :(得分:0)
如果“related_sites”元素中没有数组,请尝试:
"related_sites" : \[(?:[^\]]*\n?)*\]