String regex = "[hc]at";
String input = "The cat wears a hat.";
System.out.println("Regex: " + regex);
System.out.println("Input: " + input);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
boolean found = false;
while (matcher.find()) {
System.out.println("Found '" + matcher.group() +
"' from " + matcher.start() +
" to " + matcher.end());
found = true;
}
if(!found)
System.out.println("No match found!");
String regex = "\\d{3}-\\d{3}-\\d{4}";
String input = "415-555-1234";
System.out.println("Regex: " + regex);
System.out.println("Input: " + input);
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
if (matcher.matches()) {
System.out.println("Match!");
} else {
System.out.println("Does not match!");
}