-1

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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
ShareYourExp
  • 11
  • 1
  • 1
  • 5

2 Answers2

0

Use a negative look ahead, anchored to start, for “zero”. Here’s one way:

^(?!0*\.00)\d+\.\d\d$

The subexpression (?!0*\.00) means “what follows must not be any number of 0’s (including none) then .00".

Bohemian
  • 412,405
  • 93
  • 575
  • 722
0

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
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37