Match address regular expressions : Match Address « Regular Expressions « Java






Match address regular expressions

Match address regular expressions


public class MatchAddress {
  public static void main(String args[]) {

    isAddressValid("John Smith 888 Luck Street,NY 64332");
    isAddressValid("John A. Smith 888 Luck Street, NY 64332-4453");
    isAddressValid("John Allen Smith 888 Luck Street, NY 64332-4453");
    isAddressValid("888 Luck Street, NY 64332");
    isAddressValid("P.O. BOX 888 Luck Street, NY 64332-4453");
    isAddressValid("John Allen Smith 888 Luck st., NY");
  }

  public static boolean isAddressValid(String addr) {
    boolean retval = false;

    String nameToken = "\\p{Upper}(\\p{Lower}+\\s?)";

    String namePattern = "(" + nameToken + "){2,3}";

    String zipCodePattern = "\\d{5}(-\\d{4})?";

    String addressPattern = "^" + namePattern + "\\w+ .*, \\w+ "
        + zipCodePattern + "$";

    retval = addr.matches(addressPattern);

    String msg = "NO MATCH\npattern:\n " + addr + "\nregexLength:\n "
        + addressPattern;

    if (retval) {
      msg = "MATCH\npattern:\n " + addr + "\nregexLength:\n "
          + addressPattern;
    }
    System.out.println(msg + "\r\n");
    return retval;
  }
}

           
       








Related examples in the same category

1.Match Email addressMatch Email address
2.Match Address: mixing numbers and characters