• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Validate Zip code using reqular expression

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alan Moore:



:-) Much much much better!
 
author and jackaroo
Posts: 12200
280
Mac IntelliJ IDE Firefox Browser Oracle C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
reply
    Bookmark Topic Watch Topic
  • New Topic