我有一个JSON字符串:
{"dependencies":["xz","pkg-config","glib","gobject-introspection"],"conflicts_with":[],"caveats":null,"options":[{"option":"--universal","description":"Build a universal binary"}]}
我写了一个正则表达式来查找"依赖":
背后的数组(?<=\"dependencies\":).*[^:](?=,)
Java中的:
"(?<=\\\"dependencies\\\":).*[^:](?=,)"
然而结果却是:
["xz","pkg-config","glib","gobject-introspection"],"conflicts_with":[],"caveats":null,"options":[{"option":"--universal"
只排除了最后一个结肠。
请帮帮我。
答案 0 :(得分:2)
我建议使用这个正则表达式:
(?<=\"dependencies\":).[^:]*(?=,)
或者,几乎相同:
(?<=\"dependencies\":)[^:]+(?=,)
答案 1 :(得分:1)
你可以使用非贪婪的零或多量词:
(?<=\"dependencies\":)\[(.*?)\]
这将与提供的JSON中的["xz","pkg-config","glib","gobject-introspection"]
匹配。
答案 2 :(得分:0)
这让人想到parsing HTML with a regular expression。您是否尝试过使用JSON Parser?
String jsonString = "{\"dependencies\":[\"xz\",\"pkg-config\",\"glib\",\"gobject-introspection\"],\"conflicts_with\":[],\"caveats\":null,\"options\":[{\"option\":\"--universal\",\"description\":\"Build a universal binary\"}]}";
JsonReader jsonReader = Json.createReader(new StringReader( jsonString ));
JsonObject obj = jsonReader.readObject();
jsonReader.close();
String dependencies = obj.getJsonArray( "dependencies" ).toString();