Match Name : Name « Regular Expressions « Java






Match Name

Match Name
 


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

    isNameValid("John Smith");
    isNameValid("John McGee");
    isNameValid("John Willliam Smith");
    isNameValid("John Q Smith");
    isNameValid("John allen Smith");
    isNameValid("John");
  }

  public static boolean isNameValid(String name) {
    boolean retval = false;
    String nameToken = "\\p{Upper}(\\p{Lower}+\\s?)";
    String namePattern = "(" + nameToken + "){2,3}";
    retval = name.matches(namePattern);

    String msg = "NO MATCH: pattern:" + name + "\r\n           regex :"
        + namePattern;

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

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

           
         
  








Related examples in the same category

1.Pattern Matches file name