Author
Validate Zip code using reqular expression
mike nu
Ranch Hand
Joined: Mar 11, 2001
Posts: 63
posted Jul 22, 2006 21:19:00
0
Hi there, I am trying to use regular expression to validate a zip code, but could not find a good solution. This is my requirements: 1. The zip code should be 5 digits. 2. The zip code should not be "00000", "88888" or "99999". How can I do this validation in a single reqular expression? Thanks, Mike
Naseem Khan
Ranch Hand
Joined: Apr 25, 2005
Posts: 809
Well It could be done as follows with full exception handling. If no exception is thrown it means zip code entered is correct. Naseem
Asking Smart Questions FAQ - How To Put Your Code In Code Tags
James Sabre
Ranch Hand
Joined: Sep 07, 2004
Posts: 781
posted Jul 23, 2006 00:03:00
0
Originally posted by mike nu: How can I do this validation in a single reqular expression?
I don't think this can be done in one regex but it can be done in several ways with two e.g. boolean isGood = value.matches("\\d{5}") && !value.matches("([089])\\1{4}");
Retired horse trader.
Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
James Sabre
Ranch Hand
Joined: Sep 07, 2004
Posts: 781
posted Jul 23, 2006 01:02:00
0
Originally posted by James Sabre: I don't think this can be done in one regex but it can be done in several ways with two e.g. boolean isGood = value.matches("\\d{5}") && !value.matches("([089])\\1{4}");
Having said I don't think this can be done I find it can! boolean isGood = value.matches("[1-7]\\d{4}" + "|0[1-9]\\d{3}|8[0-79]|9[0-8]\\d{3}" + "|00[1-9]\\d{2}|88[0-79]\\d{2}|99[0-8]\\d{2}" + "|000[1-9]\\d|888[0-79]\\d|999[0-8]\\d" + "|0000[1-9]|8888[0-79]|9999[0-8]"); Not nice but it can be done!
James Sabre
Ranch Hand
Joined: Sep 07, 2004
Posts: 781
posted Jul 23, 2006 01:06:00
0
Originally posted by James Sabre: Having said I don't think this can be done I find it can! boolean isGood = value.matches("[1-7]\\d{4}" + "|0[1-9]\\d{3}|8[0-79]|9[0-8]\\d{3}" + "|00[1-9]\\d{2}|88[0-79]\\d{2}|99[0-8]\\d{2}" + "|000[1-9]\\d|888[0-79]\\d|999[0-8]\\d" + "|0000[1-9]|8888[0-79]|9999[0-8]"); Not nice but it can be done!
Which needs a correction! value.matches("[1-7]\\d{4}" + "|0[1-9]\\d{3}|8[0-79]\\d{3}|9[0-8]\\d{3}" + "|00[1-9]\\d{2}|88[0-79]\\d{2}|99[0-8]\\d{2}" + "|000[1-9]\\d|888[0-79]\\d|999[0-8]\\d" + "|0000[1-9]|8888[0-79]|9999[0-8]");
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35220
posted Jul 23, 2006 02:22:00
0
Originally posted by James Sabre: Having said I don't think this can be done I find it can!
Crafty! Although it might make a good entry for the "Obfuscated Regular Expression Contest" if there were such a thing
Android apps – ImageJ plugins – Java web charts
Alan Moore
Ranch Hand
Joined: May 06, 2004
Posts: 262
posted Jul 23, 2006 03:45:00
0
"Obfuscated Regular Expression Contest"? That's even more redundant than an obfuscated Perl contest.
James Sabre
Ranch Hand
Joined: Sep 07, 2004
Posts: 781
posted Jul 23, 2006 04:09:00
0
Originally posted by Alan Moore:
:-) Much much much better!
Andrew Monkhouse
author and jackaroo
Marshal Commander
Joined: Mar 28, 2003
Posts: 10812
Originally posted by Alan Moore: "Obfuscated Regular Expression Contest"? That's even more redundant than an obfuscated Perl contest.
subject: Validate Zip code using reqular expression