Home Regularexpressions Matching Zip code using regular expression



Matching Zip code using regular expression
Posted on: August 30, 2008 at 12:00 AM
This Example describes the way of matching Zip code with the regular expression .For this we are going to make program named Matching_Zipcode.java.

Matching Zip code using regular expression

     

This Example describes the way of matching Zip code with the regular expression .For this we are going to make program named Matching_Zipcode.java. The steps involved in program Matching_Zipcode.java are described below:-

String zipCode = "\\d{5}(-\\d{4})?":-This is the regular expression use to match with Zip code.

matchZip("45643-44435"):-Defining Zip code which is to be matched with the regular expression

Matching_Zipcode.java

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Matching_Zipcode {

  public static void main(String args[]) {
  matchZip("45643");
  matchZip("45643-44435");

  }
  public static boolean matchZip(String zip) {
  boolean retrival = false;
  String zipCode = "\\d{5}(-\\d{4})?";
  retrival = zip.matches(zipCode);

  String msg = "NO MATCH: Zip code is:" + zip +
  "\n regex for the Zip code is: " + zipCode;

  if (retrival) {
  msg = "MATCH : Zip code is :" + zip + 
  "\n regex for the Zip code is : " + zipCode;
  }

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

Output of the program:-

MATCH : Zip code is :45643
regex for the Zip code is : \d{5}(-\d{4})?

NO MATCH: Zip code is:45643-44435
regex for the Zip code is: \d{5}(-\d{4})?


Download Source Code

Related Tags for Matching Zip code using regular expression:
javaciozipmakeipcipexpressionnamethisforexamplewithmatchingprogrammatchtoexpressramexamssizipcodeeitdesulpcinmjesnamedmeprodescribexaxampsessatkismplpresspregoarcodcodexpvazscrssrithavbeschipcodepleplprodeonogro


More Tutorials from this section

Ask Questions?    Discuss: Matching Zip code using regular expression  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.