gpt4 book ai didi

Java,正则表达式捕获模式的多次出现

转载 作者:行者123 更新时间:2023-12-01 07:34:55 26 4
gpt4 key购买 nike

这是我原来的字符串:

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com