import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(^[2-9][0-9]{2}$)|(^[1-2][0-9]{3}$)";
final String string = "1\n"
+ "12\n"
+ "19\n"
+ "20\n"
+ "21\n"
+ "29\n"
+ "30\n"
+ "31\n"
+ "40\n"
+ "50\n"
+ "60\n"
+ "70\n"
+ "80\n"
+ "98\n"
+ "99\n"
+ "100\n"
+ "101\n"
+ "102\n"
+ "149\n"
+ "150\n"
+ "180\n"
+ "190\n"
+ "199\n"
+ "200\n"
+ "201\n"
+ "202\n"
+ "300\n"
+ "400\n"
+ "500\n"
+ "600\n"
+ "999\n"
+ "1000\n"
+ "1001\n"
+ "1002\n"
+ "2000\n"
+ "3000\n"
+ "9999\n"
+ "10000\n"
+ "10001\n"
+ "10002\n"
+ "10003\n"
+ "19999\n"
+ "20000\n"
+ "20001\n"
+ "20002\n"
+ "30000\n"
+ "50000\n"
+ "60000\n"
+ "99999\n"
+ "100000\n"
+ "100001\n"
+ "30000000\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
final Matcher matcher = pattern.matcher(string);
while (matcher.find()) {
System.out.println("Full match: " + matcher.group(0));
for (int i = 1; i <= matcher.groupCount(); i++) {
System.out.println("Group " + i + ": " + matcher.group(i));
}
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Java, please visit: https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html