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 non-word character: [^\w])
searchREGEX("\\W", " ", "1", "f35 airplane", ".", "a!");
}
}
/*
run:
Found " " from index 0 to 1
\W: "1" Not found
Found " " from index 3 to 4
Found "." from index 0 to 1
Found "!" from index 1 to 2
*/