Solved

RegEx needed to handle zip codes

Posted on 2008-10-29
2
214 Views
Last Modified: 2010-04-14
I need RegEx that will allow the following zip codes.



city            state   country ZIP
Seacliff Park   ZZ      AU      5049
Calgary         AB      CA      T2J7C9
Tokyo           ZZ      JP      1070052
Fareham         ZZ      UK      PO15 5RQ
Las Vegas       NV      US      89123-2545
0
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 13

Accepted Solution

by:
numberkruncher earned 500 total points
ID: 22836268
I think that you are going to need one regex for each country. You can store an array of regex rules and then select the one you need based upon country code. This way, you can validate a zip/postal code based upon the location. A UK postal code is invalid in the US for example.

Alternatively you could have a really long regular expression along the lines of the following, but this would not validate zip/postal codes depending upon the country.

(UK)|(AU)|(CA)...

Where UK would be interchanged with the expression below.
// Example for UK
Regex re = new Regex("[a-z][a-z][0-9][0-9]?[ \t]*[0-9][0-9]?[a-z][a-z]", RegexOptions.IgnoreCase);
Text = re.IsMatch(textBox1.Text).ToString();

Open in new window

0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22836304
In case you are unaware of what the operators I've used are:

[a-z]    - Option selector, must be one of the listed values (in this case a, b, ..., z).
?         - Indicate preceding test is optional.
*          - Indicates that there can be zero or more of previous item.
+         - Indicates that there must be at least one or more of previous item (I didn't use this one above).
|          - Alternate, either left or right test must occur, can be equivalent to option selector (a|b|c|d|...|z) or a more advanced (abc)|(def).
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question