package javaapplication1;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
// [1-3[6-9]] 1 through 3, or 6 through 9
searchREGEX("[1-3[6-9]]", "0", "2", "4", "6", "8", "9");
}
private static void searchREGEX(String regex, String... search) {
Pattern pattern = Pattern.compile(regex);
for (String input : search) {
Matcher matcher = pattern.matcher(input);
boolean found = false;
while (matcher.find()) {
System.out.println(String.format("Found" + " \"%s\" " + "from index %d to %d",
matcher.group(), matcher.start(), matcher.end()));
found = true;
}
if (!found)
System.out.println("Not found");
}
}
}
/*
run:
Not found
Found "2" from index 0 to 1
Not found
Found "6" from index 0 to 1
Found "8" from index 0 to 1
Found "9" from index 0 to 1
*/