有一串类似如下:
String strT = "round-trip (ms)  min/avg/max = 0.956/5/6.752";由于限制,现在需要三个正则表达式予以获取串中的数据。
比如,可以用"ms\\D*(\\d+\\.?\\d*)"匹配获取第一个数据0.021
类似请问第二个、第三个正则表达式咋个写?尽量简单可行。

解决方案 »

  1.   

    真的吗?我怎么试的结果是 ms)  min/avg/max = 0.956楼主,你不能认真写一下吗,你想输出的是
    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
      

  2.   

    String s="round-trip (ms)  min/avg/max = 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());
    }