Java Tutorial/Regular Expressions/Validation

Материал из Java эксперт
Перейти к: навигация, поиск

Date format consists of one or two digits followed by a hyphen, followed by one or two digits, followed by a hypen, followed by four digits

public class MainClass {
  public static void main(String args[]) {
    String date = "12-12-1212";
    String datePattern = "\\d{1,2}-\\d{1,2}-\\d{4}";
    System.out.println(date.matches(datePattern));
  }
}





Finding duplication of words

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class MainClass {
  public static void main(String args[]) {
    String phrase = "a word word";
    String duplicatePattern = "\\b(\\w+) \\1\\b";
    Pattern p = null;
    try {
      p = Pattern.rupile(duplicatePattern);
    } catch (PatternSyntaxException pex) {
      pex.printStackTrace();
      System.exit(0);
    }
    // count the number of matches.
    int matches = 0;
    // get the matcher
    Matcher m = p.matcher(phrase);
    String val = null;
    // find all matching Strings
    while (m.find()) {
      val = ":" + m.group() + ":";
      System.out.println(val);
      matches++;
    }
  }
}





Match a digit

public class MainClass {
  public static void main(String args[]) {
    String candidate = "Java 6";
    String pattern = "Java \\d";
    System.out.println(candidate.matches(pattern));
  }
}





Name validation

public class MainClass {
  public static void main(String args[]) {
    String name = "First Name";
    String nameToken = "\\p{Upper}(\\p{Lower}+\\s?)";
    String namePattern = "(" + nameToken + "){2,3}";
    System.out.println(name.matches(namePattern));
  }
}





Phone number validation

public class MainClass {
  public static void main(String args[]) {
    String phone = "123-(456)-9999";
    String phoneNumberPattern = "(\\d-)?(\\d{3}-)?\\d{3}-\\d{4}";
    System.out.println(phone.matches(phoneNumberPattern));
  }
}





Primitive address validation

public class MainClass {
  public static void main(String args[]) {
    String addr = "my Address 12345";
    String namePattern = "([A-Za-z])+ (([A-Za-z])+\\.? )?([A-Za-z])+\\s*";
    String zipCodePattern = "\\d{5}(-\\d{4})?";
    String addressPattern = "^" + namePattern + "\\w+ .*, \\w+ " + zipCodePattern + "$";
    System.out.println(addr.matches(addressPattern));
  }
}





String manipulation using English synonyms

public class MainClass {
  public static void main(String args[]) {
    String statement = "A B AA";
    String tokens[] = null;
    String splitPattern = "A|B|C|E|(G H I J)|(AA BB CC)";
    tokens = statement.split(splitPattern);
    System.out.println("REGEX PATTERN:\n" + splitPattern + "\n");
    System.out.println("STATEMENT:\n" + statement + "\n");
    System.out.println("TOKENS:");
    for (int i = 0; i < tokens.length; i++) {
      System.out.println(tokens[i]);
    }
  }
}





Zip code validation

public class MainClass {
  public static void main(String args[]) {
    String zip = "123456";
    String zipCodePattern = "\\d{5}(-\\d{4})?";
    System.out.println(zip.matches(zipCodePattern));
  }
}