import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Example {
public static void main(String[] args) {
final String regex = "(?:(?:(?:\\+|00)32\\h?(?:\\(0\\)\\h?)?|0)(?:4(?:60|[789]\\d)/?(?:\\h?\\d{2}\\.?){2}\\h?\\d{2}|(?:\\d/?\\h?\\d{3}|\\d{2}[/-]?\\h?\\d{2})(?:\\.?\\h?\\d{2}){2}))(?!\\S)";
final String string = "OK 01/07 - 31/07 \n"
+ "OK 0487207339\n"
+ "OK +32487207339\n"
+ "OK 01.07.2016\n"
+ "OK +32 (0)16 89 44 77\n"
+ "OK 016894477\n"
+ "OK 003216894477\n"
+ "OK +3216894477\n"
+ "OK 016/89.44.77\n"
+ "OK +32 16894477\n"
+ "OK 0032 16894477\n"
+ "OK +32 16/894477\n"
+ "NOK +32 16-894477 (this should match)\n"
+ "OK 0479/878810\n"
+ "NOK 20150211-0001731015-1 (this shouldn't match)";
final Pattern pattern = Pattern.compile(regex);
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