Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Validate Zip code using reqular expression

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well It could be done as follows with full exception handling.



If no exception is thrown it means zip code entered is correct.

Naseem
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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}");
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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]");
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"Obfuscated Regular Expression Contest"? That's even more redundant than an obfuscated Perl contest.
 
James Sabre
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Alan Moore:



:-) Much much much better!
 
author and jackaroo
Posts: 12199
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Alan Moore:
"Obfuscated Regular Expression Contest"? That's even more redundant than an obfuscated Perl contest.

    Bookmark Topic Watch Topic
  • New Topic