Reputation: 33
I have a regex
^(?=.*[1-9])\\d{0,5}(?:\\.\\d{0,2})?$
to check amount should be greater then 0 and it can contain maximum five digit before decimal and optional decimal and 2 digits after decimal.
Here (?=.*[1-9])
is to check there should be latest one occurrence of any digit from (1-9)
in the string.
But I want modify it to check amount greater then one(1), so I want (?=.*[1-9])
to check only till the occurrence of the decimal point i.e. 0.1 it should return false.
Other condition should also fulfill.
Note : the count of digits before decimal is not fix, it will very from 1 to 5, so we can't modify it to (?=.{0,5}[1-9])
Upvotes: 3
Views: 40
Reputation: 786091
You can fix it by using this negation based regex:
^(?=[^.]*[1-9])\\d{0,5}(?:\\.\\d{0,2})?$
[^.]*
before [1-9]
will match any character except decimal point thus not allowing 0.45
as valid number.
Upvotes: 1