import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(0|\\+98)9[0-9]{9}$";
final String string = "09156987452\n"
+ "09908472853\n"
+ "09153610882365\n"
+ "09158645287\n"
+ "+9814756842\n"
+ "+980915874622\n"
+ "+989908472853\n"
+ "+981283547\n"
+ "9836587498\n"
+ "+986859554\n"
+ "+989158497858\n"
+ "+9812457868\n"
+ "02154091563214587\n"
+ "12409156032145\n"
+ "091596356478\n"
+ "+982654789652\n"
+ "023654d\n"
+ "5588\n"
+ "879 091586424569\n"
+ "+9878546\n"
+ "+9999999999999\n"
+ "+989153610883\n"
+ "0915603551555555555\n";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
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