Przykładowa walidacja/maski za pomocą wyrażeń regularnych:
public class RegExTest {
public static void main(String[] args) {
System.out.print("\\a{5}\\d{15} ");
String path = "abcde123456789012345";
String regularExpression = "([A-Za-z]{5}+[0-9]{15})";
Pattern pattern = Pattern.compile(regularExpression);
System.out.print(pattern.matches(regularExpression, path));
regularExpression = "([A-Za-z]{5}\\d{15})";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\a{2,} ");
path = "ab";
regularExpression = "[A-Za-z]{2,}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\w{3,} ");
path = "a0_";
regularExpression = "[A-Za-z0-9_]{3,}";
regularExpression = "\\w{3,}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\d{11} ");
path = "12345678901";
regularExpression = "[0-9]{11}";
pattern = Pattern.compile(regularExpression);
System.out.print(pattern.matches(regularExpression, path)+"/");
regularExpression = "\\d{11}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("[tTnN]{1} ");
path = "t";
regularExpression = "[tTnN]{1}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("[\\w\\d[:punct:]] ");
path = "1";
regularExpression = "[\\w\\d\\p{Punct}]{1,}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\d{2}-\\d{3} ");
path = "21-010";
regularExpression = "\\d{2}-\\d{3}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\w ");
path = "gfasg4wer454351235Fw_";
regularExpression = "[A-Za-z0-9_]{1,}";
pattern = Pattern.compile(regularExpression);
System.out.print(pattern.matches(regularExpression, path)+"/");
regularExpression = "\\w{1,}";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$ ");
path = "konto@komena.pl";
regularExpression = "^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+$";
pattern = Pattern.compile(regularExpression);
System.out.println(pattern.matches(regularExpression, path));
System.out.print("\\d{9} ");
path = "123456789";
regularExpression = "\\d{9}";
pattern = Pattern.compile(regularExpression);
System.out.println(": "+pattern.matches(regularExpression, path));
String input = "linea\nlineb\nlinec";
Pattern p = Pattern.compile("(?i)lineb", Pattern.DOTALL);
Matcher m = p.matcher(input);
boolean b = m.find();
System.out.println("match found: " + b);
}
}