Creating regex to extract 4 digit number from string using java

前端 未结 7 774
余生分开走
余生分开走 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:02

    If you want to match any number of digits then use pattern like the following:

    ^\D*(\d+)\D*$
    

    And for exactly 4 digits go for

    ^\D*(\d{4})\D*$
    

提交回复
热议问题