I am trying to generate regex for the following number range…
It is mandatory that I need to have the 00001 or 0000001 as the starting number
ISRAEL NNNNN 00001 99999
NNNNNNN 0000001 9999999
I am trying with this regex: ^0*[0-9]{5}$|^0*[0-9]{7}$
But it fails for the following scenarios. It accepts these number which shouldn’t be.
0100012
000010121
I think that what OP is trying to ask is how to write a regular expression that ensures that of all 5 or 7 digits at least one is non-zero (probably zip-code can’t be all zeros).
Obviously, it is much easier to do this w/o using a regular expression. Parse integer, see if it is greater then zero and less then 100000 or 1000000, and you are done. Of course, if you want to force your users to input extra zeros in front of the code… that’s your choice. Users comply with even weirder demands nowadays.
But if you felt challenged by the task complexity, and wanted it to be done by using regular expression no matter the price, then here you go:
This would ensure that all the characters of the string are digits, and that at least one of them is non-zero. Additionally checking the length of the string, you’d figure out if it’s equal 7 or 5. (This is a relatively easy way).
The hard way:
(This is for 5-digit zip codes, you are welcome to try to extend it to 7-digit zip-codes) 😛