import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "([+]\\d{1,2}|(00)\\d{2,4}|\\d{2})( [(]\\d{1,3}[)])?[-. \\/]?\\d{2,6}?[-. \\/]?\\d{2,15}([-. \\/]{1,3}\\d{1,5}){0,4}";
final String string = "+49 40 123456-0\n"
+ "+49 40 123456 -0\n"
+ "+49 40 123456- 0\n"
+ "0049 40 123456-0\n"
+ "49 40 3339760\n"
+ "+49 151 1234 56 78\n"
+ "+49 40123456 - 282\n"
+ "+49 40 123 456 -282\n"
+ "0049 40 123 456 -282\n"
+ "+49 40 123456 - 282\n"
+ "+49 (0)40 123456-282\n"
+ "+49 (0) 40 123456 -282\n"
+ "+49 (0) 40 12 34 56 -282\n"
+ "0049 (0) 40 123456 -282\n"
+ "49 (0) 40 123456 -282\n"
+ "+4940123456282\n"
+ "0049401234560\n"
+ "+1-234-567-8901\n"
+ "+61-234-567-89-01\n"
+ "+46-234 5678901\n"
+ "+1 (234) 56 89 901\n"
+ "+1 (234) 56-89 901\n"
+ "+46.234.567.8901\n"
+ "+1/234/567/8901\n"
+ "Wenn man mich fragen würde, ob ich unter der +49 40 123456 282 erreichbar bin, würde ich sagen. \"JA\"";
final Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE | Pattern.DOTALL);
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