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; } }