Reg Ex - not allowing zero
Excuse my ignorance. My knowledge of regular expressions is extremely limited.
I have the following regular expression on a regular expression validator.
^(\d{1,3},?(\d{3},?){0,2}\d{3}|\d{1,3})$
It currently allows zero. I need to update it so that it does not allow a value of 0.
[Edit: 开发者_JAVA百科Just to clarify, I would like it to exclude a value of zero rather than a singular "0" - thanks]
Any help would be greatly appreciated.
Thanks zaps
You may be looking for something like this:
^[1-9]\d{0,2}(\d*|(,\d{3})*)$
0
is allowed by the second part of your regex. Change it to:
^(\d{1,3},?(\d{3},?){0,2}\d{3}|[1-9]\d{0,2})$
It makes sure that the first digit is non zero, when the total number of digits are less than or equal to three.
The regex still allows patterns like 000,000,000
and 000,123
To fix that you can change the first part of the regex to:
^([1-9]\d{0,2},?(\d{3},?){0,2}\d{3}|[1-9]\d{0,2})$
Or rewrite it as
^[1-9]\d{0,2}(,?\d{3}){0,3}$
This still allows 123,456789
and 123456,789
. Let us change it to:
^[1-9]\d{0,2}(?:(,\d{3}){0,3}|(\d{3}){0,3})$
This will allow 123,456,789
and 123456789
but not 123,456789
or 123456,789
精彩评论