String[] strings = { //False "test-metareg-rw40lntknahvpseba32cßáàâåäæç", "test-metareg--rw40lntknahvpseba32cßáàâåäæç", "test-metareg-rw40lntknahvpseba32c-", "12345678901234567890123456789012345678901234567890123456789012-", "123456789012345678901234567890123456789012345678901234567890124-", //True "test-metareg-rw40lntknahvpseba32c", "testmetareg", "123456789012345678901234567890123456789012345678901234567890123", }; for (String string : strings) { Pattern compile = Pattern.compile("(?=[a-z0-9-]{1,63}$)([a-z0-9]++-){0,63}[a-z0-9]+"); Matcher matcher = compile.matcher(string); boolean matches = matcher.matches(); //Takes 30+ seconds System.out.println(matches); }