import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "^(\\+55)?[\\s]?\\(?(\\d{2})?\\)?[\\s-]?(9?\\d{4}[\\s-]?\\d{4})$";
final String string = "+55(16)91555-3456\n"
+ "22 98671 1032\n"
+ "+55 7134764819\n"
+ "(23)99378-4366\n"
+ "+5598683 1021\n"
+ "+551199603-3412\n"
+ "55 98214 9064\n"
+ "(55)98330-7156\n"
+ "(82) 936147156\n"
+ "+552430431084\n"
+ "2140028922\n"
+ "40028922\n"
+ "21 40028922\n"
+ "(21) 4002-8922\n"
+ "+55 21 4002 8922\n"
+ "+55(21)4002-8922\n"
+ "+55986320291";
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