import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^5[1-5]\\d{14}$|^2(?:2(?:2[1-9]|[3-9]\\d)|[3-6]\\d\\d|7(?:[01]\\d|20))\\d{12}$";
final String string = "1221009999999999\n"
+ "2121009999999999\n"
+ "2211009999999999\n"
+ "2220009999999999\n"
+ "2221009999999999\n"
+ "2321009999999999\n"
+ "2499999999999999\n"
+ "2720999999999999\n"
+ "2721009999999999\n"
+ "2721999999999999\n"
+ "2730999999999999\n"
+ "2820999999999999\n"
+ "4111111111111111\n"
+ "5055555555555555\n"
+ "5100000000000000\n"
+ "5255555555555555\n"
+ "5444444444444444\n"
+ "5555555555555555\n"
+ "5600000000000000\n"
+ "5911111111111111";
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