gpt4 book ai didi

java - 如何提取3个键之间的数值?

转载 作者:太空宇宙 更新时间:2023-11-04 12:29:33 31 4
gpt4 key购买 nike

我有这个字符串:页面:4,响应:5,alt:99

我想将值提取到数组中:

[4,5,99]

这是我的代码,有没有办法改进它?

pattern = "page: d+?,response: d+?, alt: d+?";

String first = origianlString.replaceAll(pattern, "$0");
String second = origianlString.replaceAll(pattern, "$1");
String third = origianlString.replaceAll(pattern, "$2");

最佳答案

你可以试试这个:

String regex = "\\D+?(\\d+)\\D+?(\\d+)\\D+?(\\d+)";
String input = "page: 4, response: 5, alt: 99";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
if (m.find()) {
String first = m.group(1);
String second = m.group(2);
String third = m.group(3);
System.out.println("[" + first + "," + second + "," + third+ "]");
}

输出:

[4,5,99]

关于java - 如何提取3个键之间的数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37997924/

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