javaregexvalidation

Regex expression for 8 digits before decimal and 2 after decimal and whole string should not compute to zero


I need regex expression for the following :

\d{0,8}(\.\d{1,2})?$

this works for the above case , but I don't want the following strings to be accepted. The string should not result to zero.


Solution

  • Another option is to assert not only dots and zeroes

    ^(?![0.]*$)\d{0,8}(?:\.\d{1,2})?$
    

    Regex demo | Java demo

    In Java

    String regex = "^(?![0.]*$)\\d{0,8}(?:\\.\\d{1,2})?$";
    

    If you want a match only, you can omit the capturing group and make it non capturing (?: