By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,933 Members | 1,716 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,933 IT Pros & Developers. It's quick & easy.

reg exp help needed

P: 6
I want to validate the postal codes for 15 different countries. For
this I have written the perl script shown below. In this script postal
codes for Netherland, belgium, Italy, Ireland can be validated with
the use of regular expressions. Still I need to validate countries
like Greece, Norway, United Kingdom, Sweden, Finland, Schweiz,
Portugal, Austria, Luxembourg, France, Spain, Deutschland, Denmark. I
am not getting the regular expressions for these countries.
Can anyone help me out for these regular expressions??

Expand|Select|Wrap|Line Numbers
  1. $country = $ARGV[0];
  2. $zip = $ARGV[1];
  3. $countrycode = "";
  4. $invalid_zip = "";
  5.  
  6. if($country =~ m/Netherland/)
  7. {
  8.     $invalid_zip =1  unless ($zip =~ /[1-9][0-9]00 NL/);
  9.     print "$country\t$zip\n;"
  10. }
  11. elsif($country =~ /Belgium/)
  12. {
  13.  
  14.     $invalid_zip =1  unless ($zip =~ /^([1-9]{1}[0-9]{3}$)/i);
  15.  
  16.     print "$country\t$zip\n;"
  17. }
  18. elsif($country =~ /Italy/)
  19. {
  20.     $invalid_zip =1  unless ($zip =~ /^[0-9]{5}$/i);
  21.     print "$country\t$zip\n;"
  22. }
  23. elsif($country =~ /Ireland/)
  24. {
  25.     $invalid_zip =1  unless ($zip =~ /^\s*$|D\d+w?/i);  ##Ireland does not have postal codes except for Dublin City
  26.     print "$country\t$zip\n";
  27. }
  28. elsif($country =~ /Greece/)
  29. {
  30.     $invalid_zip =1  unless ($zip =~ /^[1-8][0-9]{2}$|^[1-8][0-9]{2}[0-9]{2}$/i);
  31.     print "$country\t$zip\n";
  32. }
  33. elsif($country =~ /Norway/)
  34. {
  35.     $invalid_zip =1  unless ($zip =~ /^[0-9]{4}$/i);
  36.     print "$country\t$zip\n";
  37. }
  38.  
  39.  
  40. if($invalid_zip)
  41. {
  42.  
  43.     print "Invalid Postal Code";
  44. }
May 27 '08 #1
Share this Question
Share on Google+
1 Reply


KevinADC
Expert 2.5K+
P: 4,059
What is your question? What the zip code for those nations are? That is up to you to find out, once you know the zip code format you can write a regexp to match it.
May 27 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.