Home > Mobile >  Regex to validate only digits from 0-9, maximum of 8 digits till a dot and only two decimals as maxi
Regex to validate only digits from 0-9, maximum of 8 digits till a dot and only two decimals as maxi

Time:09-10

I haven't used regular expressions at all, so I'm having difficulty troubleshooting. I'm looking for a regex that can help me to validate if there are only digits from 0-9, only 8 digits till a dot and two decimals as maximum.

For example:

33445566.09

/[0-9]/

CodePudding user response:

/[0-9]{1,8}\.[0-9]{1,2}/

If you want to just match or not

[0-9] means match 0 or 1 or ... 9

{1,8} means it can match minimum 1 and maximum 8

\. means escape .

If you want to get the group just use () in the regex

/([0-9]{1,8})\.([0-9]{1,2})/

This way you can get 33445566 and 09

CodePudding user response:

Try this:

    String input = "123456789.12";
    Pattern pattern = Pattern.compile("^[0-9]{1,8}\\.[0-9]{1,2}$");
    Matcher matcher = pattern.matcher(input);
    System.out.println(matcher.matches()); //false
  • ^ start of line
  • $ end of line
  • [0-9] only 0123456789
  • {1,8} symbols count from 1 to 8 include
  • Related