import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
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;
}
System.out.println();
if (!found) {
System.out.println(String.format("%s: \"%s\" Not found\n", regex, input));
}
}
}
public static void main(String[] args) {
// \w (A word character: [a-zA-Z_0-9])
searchREGEX("\\w", " ", "1", "f35 airplane", ".", "a!");
}
}
/*
run:
\w: " " Not found
Found "1" from index 0 to 1
Found "f" from index 0 to 1
Found "3" from index 1 to 2
Found "5" from index 2 to 3
Found "a" from index 4 to 5
Found "i" from index 5 to 6
Found "r" from index 6 to 7
Found "p" from index 7 to 8
Found "l" from index 8 to 9
Found "a" from index 9 to 10
Found "n" from index 10 to 11
Found "e" from index 11 to 12
\w: "." Not found
Found "a" from index 0 to 1
*/