有一字符串str可能为100 days, 20 hours, 18 minutes, 27 seconds
也可能是20 hours, 18 minutes, 27 seconds
也可能是18 minutes, 27 seconds
也可能是27 seconds现求一个正则表达式获取这个字符串的数字.
请高手作答?

解决方案 »

  1.   

    public static void main(String args[]) {
    String s = "100 days, 20 hours, 18 minutes, 27 seconds";
    String a = "[a-zA-Z]*";
    System.out.println(s.replaceAll(a, ""));
    }
    结果:100 , 20 , 18 , 27
      

  2.   


    public static void main(String[] args) {
    String str = "100 days, 20 hours, 18 minutes, 27 seconds";
    String reg = "(\\d+)";
    Matcher matcher = Pattern.compile(reg).matcher(str);
    while(matcher.find()){
    System.out.println(matcher.group());
    }
    }