请帮我纠正我的代码
Pattern p = Pattern.compile("\\{(.*)\\}");
String value = "only test {g_url} hahaha {l_user} alallala {l_password} test2";
Matcher m = p.matcher(value);
while (m.find()) {
System.out.println("json string= " + m.group(1));
}
系统打印
json string= g_url} hahaha {l_user} alallala {l_password
我想要 系统打印
json string= g_url
json string= l_user
json string= l_password
答案 0 :(得分:0)
默认情况下.*
是贪婪的,并且会选择所有字符串直到最后一个结束括号。为避免这种情况,您需要将模式定义为:
Pattern p = Pattern.compile("\\{(.*?)\\}");
OR
Pattern p = Pattern.compile("\\{([^\\}]*)\\}")