package javaapplication1;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
// X? , X?? and X?+ (X once or not at all)
searchREGEX("", "a?", "a??", "a?+");
}
private static void searchREGEX(String input, String... regex_list) {
for (String regex : regex_list) {
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(input);
boolean found = false;
while (matcher.find()) {
System.out.println(String.format("%s: Found" + " \"%s\" " + "from index %d to %d",
regex, matcher.group(), matcher.start(), matcher.end()));
found = true;
}
System.out.println();
if (!found)
System.out.println("Not found");
}
}
}
/*
run:
a?: Found "" from index 0 to 0
a??: Found "" from index 0 to 0
a?+: Found "" from index 0 to 0
*/