Match Zip : ZIP Code « Regular Expressions « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Regular Expressions » ZIP Code 




Match Zip
Match Zip
 
/*
#Japanese postal codes
zipJP=^\d{3}-\d{4}$

#US postal codes
zipUS=^\d{5}\p{Punct}?\s?(?:\d{4})?$

#Dutch postal code
zipNL=^[0-9]{4}\s*[a-zA-Z]{2}$

#Argentinean postal code
zipAR=^\d{3}-\d{4}$

#Swedish postal code
zipSE=^(s-|S-){0,1}[0-9]{3}\s?[0-9]{2}$

#Canadian postal code
zipCA=^([A-Z]\d[A-Z]\s\d[A-Z]\d)$

#UK postal code
zipUK=^[a-zA-Z]{1,2}[0-9][0-9A-Za-z]{0,1} {0,1}[0-9][A-Za-z]{2}$


*/

public class MatchZipCodes {
  public static void main(String args[]) {
    isZipValid("45643-4443");
    isZipValid("45643");
    isZipValid("443");
    isZipValid("45643-44435");
    isZipValid("45643 44435");
  }

  public static boolean isZipValid(String zip) {
    boolean retval = false;
    String zipCodePattern = "\\d{5}(-\\d{4})?";
    retval = zip.matches(zipCodePattern);

    String msg = "NO MATCH: pattern:" + zip + "\r\n             regex: "
        + zipCodePattern;

    if (retval) {
      msg = "MATCH   : pattern:" + zip + "\r\n             regex: "
          + zipCodePattern;
    }

    System.out.println(msg + "\r\n");
    return retval;
  }
}

           
         
  














Related examples in the same category
1.Match Zip Codes
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.