有一串类似如下:
String strT = "round-trip (ms) min/avg/max = 0.956/5/6.752";由于限制,现在需要三个正则表达式予以获取串中的数据。
比如,可以用"ms\\D*(\\d+\\.?\\d*)"匹配获取第一个数据0.021
类似请问第二个、第三个正则表达式咋个写?尽量简单可行。
String strT = "round-trip (ms) min/avg/max = 0.956/5/6.752";由于限制,现在需要三个正则表达式予以获取串中的数据。
比如,可以用"ms\\D*(\\d+\\.?\\d*)"匹配获取第一个数据0.021
类似请问第二个、第三个正则表达式咋个写?尽量简单可行。
0.956
5
6.752
这三个数吗?如果是的话 public static void main(String[] args) { String strT = "round-trip (ms) min/avg/max = 0.956/5/6.752";
String[] regex = new String[]{"\\d\\.?\\d*(?=/.*/)","(?<=/)\\d\\.?\\d*(?=/)","\\d\\.?\\d*$"};
Pattern p = null;
Matcher m = null;
for (int i =0;i < 3; i++) {
p = Pattern.compile(regex[i]);
m = p.matcher(strT);
while (m.find()) {
System.out.print(m.group());
}
System.out.println();
}
}输出
0.956
5
6.752
String regex="\\d+\\.?\\d*";
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(s);
while(m.find()){
System.out.println(m.group());
}