Search code examples
regexreq

Req ex for 9+2 Numerci plus decimal values


I am looking for a regular expression with the following requirements:

  1. 9 + 2 after decimal
  2. If amount is zero, it should be invalid

I tried ^[1-9][0-9]*$ but it does work.


Solution

  • Use

    ^(?![0.]+$)\d{1,9}\.\d{2}$
    

    See proof

    Explanation

    --------------------------------------------------------------------------------
      ^                        the beginning of the string
    --------------------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
    --------------------------------------------------------------------------------
        [0.]+                    any character of: '0', '.' (1 or more
                                 times (matching the most amount
                                 possible))
    --------------------------------------------------------------------------------
        $                        before an optional \n, and the end of
                                 the string
    --------------------------------------------------------------------------------
      )                        end of look-ahead
    --------------------------------------------------------------------------------
      \d{1,9}                  digits (0-9) (between 1 and 9 times
                               (matching the most amount possible))
    --------------------------------------------------------------------------------
      \.                       '.'
    --------------------------------------------------------------------------------
      \d{2}                    digits (0-9) (2 times)
    --------------------------------------------------------------------------------
      $                        before an optional \n, and the end of the
                               string