import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(0|84)(2(0[3-9]|1[0-6|8|9]|2[0-2|5-9]|3[2-9]|4[0-9]|5[1|2|4-9]|6[0-3|9]|7[0-7]|8[0-9]|9[0-4|6|7|9])|3[2-9]|5[5|6|8|9]|7[0|6-9]|8[0-6|8|9]|9[0-4|6-9])([0-9]{7})$";
final String string = "84386648412\n"
+ "0386648412\n"
+ "0985391168\n"
+ "0777759299\n"
+ "02333562333\n"
+ "02835399144\n"
+ "02123014548\n"
+ "02403261236\n"
+ "02333851623\n"
+ "02163870688\n"
+ "02343954314\n"
+ "02283640309\n"
+ "02973841184\n"
+ "02973523165\n"
+ "02263576098\n"
+ "02043590527\n"
+ "0934607056\n"
+ "0551236592\n"
+ "84586184285\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