Regex for German Zip Codes

Posted on Tue 05 July 2016 in General Software Development

I found the following regular expression (source) working fine for me:

^(?!01000|99999)(0[1-9]\\d{3}|[1-9]\\d{4})$

I tried two alternatives that did not work because they were too general:

[0-9]{5}

^([0]{1}[1-9]{1}|[1-9]{1}[0-9]{1})[0-9]{3}$

The first regex matches the zip code "00000" which is invalid in the German system. The second regex matches the zip code "99999" which is invalid, too.