这是我原来的字符串:
String response = "attributes[{"id":50,"name":super},{"id":55,"name":hello}]";
我正在尝试解析字符串并提取所有 id
值,例如
50
55
Pattern idPattern = Pattern.compile("{\"id\":(.*),");
Matcher matcher = idPattern.matcher(response);
while(matcher.find()){
System.out.println(matcher.group(1));
}
当我尝试打印该值时,出现异常:
java.util.regex.PatternSyntaxException:非法重复
过去对正则表达式没有太多经验,但在网上找不到简单的解决方案。
感谢任何帮助!
最佳答案
Pattern.compile("\"id\":(\\d+)");
关于Java,正则表达式捕获模式的多次出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13293434/