how to extract numeric values from input string in java

后端 未结 15 1994
说谎
说谎 2020-12-13 16:25

How can I extract only the numeric values from the input string?

For example, the input string may be like this:

String str=\"abc d 1234567890pqr 548         


        
15条回答
  •  隐瞒了意图╮
    2020-12-13 17:04

    String str=" abc d 1234567890pqr 54897";
    Pattern pattern = Pattern.compile("\\w+([0-9]+)\\w+([0-9]+)");
    Matcher matcher = pattern.matcher(str);
    for(int i = 0 ; i < matcher.groupCount(); i++) {
      matcher.find();
      System.out.println(matcher.group());
    }
    

提交回复
热议问题