我有这个字符串:页面: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/