import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(02|03|04|06|08|09|072|074|076|077|078|079|050|051|052|053|054|055|056|058|059)((?:(?![1,0]{1}))\\d{7})$";
final String string = "Valid: \n"
+ "022345677\n"
+ "032345677\n"
+ "042345677\n"
+ "062345677\n"
+ "082345677\n"
+ "092345677\n"
+ "0722345677\n"
+ "022345677\n"
+ "0742345677\n"
+ "0762345677\n"
+ "0742345677\n"
+ "0762345677\n"
+ "0772345677\n"
+ "0782345677\n"
+ "0792345677\n"
+ "0502345677\n"
+ "0512345677\n"
+ "0522345677\n"
+ "0532345677\n"
+ "0542345677\n"
+ "0552345677\n"
+ "0562345677\n"
+ "0582345677\n"
+ "0592345677\n\n"
+ "NotValid : \n"
+ "021345677\n"
+ "030345677\n"
+ "142345677\n"
+ "362345677\n"
+ "282345677\n"
+ "492345677\n"
+ "5722345677\n"
+ "622345677\n"
+ "7742345677\n"
+ "8762345677\n"
+ "9742345677\n\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