Creating regex to extract 4 digit number from string using java

前端 未结 7 759
余生分开走
余生分开走 2021-01-20 22:26

Hi I am trying to build one regex to extract 4 digit number from given string using java. I tried it in following ways:

String mydata = \"get the 0025 data f         


        
相关标签:
7条回答
  • 2021-01-20 23:05

    Many better answers, but if you still have to use in the same way.

    String mydata = "get the 0025 data from string";
    Pattern pattern = Pattern.compile("(?<![-.])\\b[0-9]+\\b(?!\\.[0-9])");
    Matcher matcher = pattern.matcher(mydata);
    String val = "";
        if (matcher.find()) {
            System.out.println(matcher.group(0));
    
            val = matcher.group(0);
        }
    

    changed matcher.group(1); to matcher.group(0);

    0 讨论(0)
提交回复
热议问题