aspose file tools
The moose likes Java in General and the fly likes Validate Zip code using reqular expression Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Validate Zip code using reqular expression" Watch "Validate Zip code using reqular expression" New topic
Author

Validate Zip code using reqular expression

mike nu
Ranch Hand

Joined: Mar 11, 2001
Posts: 63
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

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

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

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
    
    7
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 appsImageJ pluginsJava web charts
Alan Moore
Ranch Hand

Joined: May 06, 2004
Posts: 262

"Obfuscated Regular Expression Contest"? That's even more redundant than an obfuscated Perl contest.
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Originally posted by Alan Moore:



:-) Much much much better!
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 10812
    
  24

Originally posted by Alan Moore:
"Obfuscated Regular Expression Contest"? That's even more redundant than an obfuscated Perl contest.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validate Zip code using reqular expression
 
Similar Threads
About fading text in regular System.out.println form
Please share any Java code to do email validation
Regular Expression?
regular expression
Static XML or web service?