Regular Expression German ZIP-Codes
Solution 1
You were close:
^0[1-9]\d\d(?<!0100)0|0[1-9]\d\d[1-9]|[1-9]\d{3}[0-8]|[1-9]\d{3}(?<!9999)9$
But if you can just do a simpler regex and then use a separate numerical comparison, that'd probably be easier to read.
Alternatively, a simpler version:
^(?!01000|99999)(0[1-9]\d{3}|[1-9]\d{4})$
(The simpler version is just "take the numbers 01000
-99999
and remove the two ends via a lookahead.)
Solution 2
The fastest way is just to check if string is made of 5 digits and then check if it is in specified range:
if ( preg_match('/^\d{5}$/', $input) && (int) $input > 1000 && (int) $input < 99999 ) {}
Solution 3
\b(?!01000)(?!99999)(0[1-9]\d{3}|[1-9]\d{4})\b
Edit: corrected, thanks to Hein.
Comments
-
Hein over 1 year
I give up. I need a (PHP) regular expression that matches only 5 digit numbers starting from 01001 up to 99998.
So, invalid is for example 1234, but not 01234. Also 01000 is invalid, 01002 is not, and so on. Any other 5 digit number except 99999 is valid.
What I have is the following regular expression, which does what I require - except that it still matches 99999.
Can anyone help out? Thanks...
^01\d\d[1-9]|[1-9]\d{3}[(?<=9999)[0-8]|[0-9]]$
Update
I am sorry, everybody, but things are more complex. I did not explain correctly. German zip code can be also 04103 for example (see a list of some further examples here)
-
hsz over 12 yearsWith this example you can also match
111111111
(longer than 5 digits). -
Legolas over 12 yearsI think you should add the begin and end of string markers (^ and $).
-
JJJ over 12 yearsIt works without the markers (because anything longer than 5 digits is > 99999) but edited anyway...
-
Legolas over 12 yearsYes, but the convertion to int may not like it if this matches "a11111", I'd guess.
-
Hein over 12 yearsVery good with regards to 99999 - but unfortunately it does not match 20259, my former zip code... :-(
-
Amber over 12 years@Hein - Seems to match just fine? ideone.com/2N0OP (Note that there was a typo'd
]
just before the$
before; I edited it out.) -
Hein over 12 yearsWork's perfectly - except that I just found I'm an idiot and did not explain german zips correctly - it needs to match 04109, too... ;´-(
-
Legolas over 12 years
^01\d{2}[1-9]|0[2-9]\d{3}|[1-9]\d{3}[0-8]|[1-9]\d{3}(?<!9999)9$
-
Amber over 12 years@Legolas Your version wouldn't match
01100
. The version I edited in does, though. -
Hein over 12 yearsThis looks pretty cool...! I'll try to check it against all german zips I'm able to generate and will let you know here.... Thanks!
-
Amber over 12 yearsThe simpler version in action: regexpal.com/…
-
Hein over 12 yearsCool - this is it! Thanks to all and especially to Amber!
-
hsz over 12 yearsSure.. And next developer if will take a look at this code will just say: oh crap
-
John over 4 yearsYes and you should give a SOURCE when you post the code of someone else. In this case the original source was and still is:pixelenvision.com/1708/…
-
dewey about 2 yearsIs there a way without lookahead/lookbehind? Because on macOS they are not supported: stackoverflow.com/questions/51568821/…