Regular Expression German ZIP-Codes

10,049

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.

Share:
10,049
Hein
Author by

Hein

I'm a nonprofessional enthusiast... ;o)

Updated on July 28, 2022

Comments

  • Hein
    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
    hsz over 12 years
    With this example you can also match 111111111 (longer than 5 digits).
  • Legolas
    Legolas over 12 years
    I think you should add the begin and end of string markers (^ and $).
  • JJJ
    JJJ over 12 years
    It works without the markers (because anything longer than 5 digits is > 99999) but edited anyway...
  • Legolas
    Legolas over 12 years
    Yes, but the convertion to int may not like it if this matches "a11111", I'd guess.
  • Hein
    Hein over 12 years
    Very good with regards to 99999 - but unfortunately it does not match 20259, my former zip code... :-(
  • Amber
    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
    Hein over 12 years
    Work'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
    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
    Amber over 12 years
    @Legolas Your version wouldn't match 01100. The version I edited in does, though.
  • Hein
    Hein over 12 years
    This 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
    Amber over 12 years
    The simpler version in action: regexpal.com/…
  • Hein
    Hein over 12 years
    Cool - this is it! Thanks to all and especially to Amber!
  • hsz
    hsz over 12 years
    Sure.. And next developer if will take a look at this code will just say: oh crap
  • John
    John over 4 years
    Yes 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
    dewey about 2 years
    Is there a way without lookahead/lookbehind? Because on macOS they are not supported: stackoverflow.com/questions/51568821/…