Pattern pattern = Pattern.compile("\\d+");
System.out.println("Pattern: '\\d+'");
String input = "123A45B6";
System.out.println("Inout 1: " + input);
System.out.println("--- Result #1 -------");
Matcher matcher = pattern.matcher(input);
//System.out.println("matches()? : " + matcher.matches()); // matcher.matches() will spoil the cursor
while(matcher.find()){
System.out.println("found at : " + matcher.start());
System.out.println("found end : " + matcher.end());
System.out.println("found what : " + matcher.group());
}
System.out.println("matches()? : " + matcher.matches());
System.out.println("\n=============================\n");
pattern = Pattern.compile("\\d+");
System.out.println("Pattern: '\\d+'");
input = "123456";
System.out.println("Inout 2: " + input);
System.out.println("--- Result #2 -------");
matcher = pattern.matcher(input);
//System.out.println("matches()? : " + matcher.matches()); // matcher.matches() will spoil the cursor
while(matcher.find()){
System.out.println("found at : " + matcher.start());
System.out.println("found end : " + matcher.end());
System.out.println("found what : " + matcher.group());
}
System.out.println("matches()? : " + matcher.matches());
Pattern: '\d+'
Inout 1: 123A45B6
--- Result #1 -------
found at : 0
found end : 3
found what : 123
found at : 4
found end : 6
found what : 45
found at : 7
found end : 8
found what : 6
matches()? : false
=============================
Pattern: '\d+'
Inout 2: 123456
--- Result #2 -------
found at : 0
found end : 6
found what : 123456
matches()? : true
Example: Ipv4 address
String REGEX_IPv4 = "(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9])" +
"\\." +
"(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)" +
"\\." +
"(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[1-9]|0)" +
"\\." +
"(25[0-5]|2[0-4][0-9]|[0-1][0-9]{2}|[1-9][0-9]|[0-9])";
Example: Ipv6 address
String REGEX_IPv6 = "(?i)" + "[[a-f][0-9]:/.+*%\\[\\]]+";
Example: Email address
String REGEX_EMAIL = "[a-zA-Z0-9_+.%\\-]{1,256}" +
"\\@" +
"[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" +
"(" +
"\\.[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" +
")+";
Example: WEB URL
String WEB_URL = "(?i)" +
"\\b(https?|ftp|file)://" +
"[a-zA-Z0-9+&@#/%?=~_|!:,.;\\-]*[a-zA-Z0-9+&@#/%=~_|\\-]";
Example: Password strength validity
The password strength criteria requires:
8+ characters length
1+ Special Character
1+ letters in Upper Case
2+ numerals (0-9)
3+ letters in Lower Case
String REGEX_PWD =
"^" + // starting
"" + // empty string
"(?=.*[\\W])" + // followed by at least 1 special char
"(?=.*[A-Z])" + // followed by at least 1 upper-case char
"(?=.*[0-9].*[0-9])" + // followed by at least 2 digits
"(?=.*[a-z].*[a-z].*[a-z])" + // followed by at least 3 lower-case chars
".{8,}" + // followed by any (.) chars with total length of at least 8
"$"; // ending