I am looking for a regular expression with the following requirements:
- 9 + 2 after decimal
- If amount is zero, it should be invalid
I tried ^[1-9][0-9]*$
but it does work.
I am looking for a regular expression with the following requirements:
I tried ^[1-9][0-9]*$
but it does work.
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
".
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