1

I am doing THIS exercise on CodeEval. I have submitted my code a number of times but the maximum score I seem to get each time is 95. There are 20 test cases(email addresses) that the site runs against a submitted code. This means that there is one particular email in which one my code is giving the wrong answer to. Below are my 2 functions (which have given me the highest score yet, of 95) for checking a valid email. The difference with each is that each time I'm trying out a different regex:

FUNCTION 1

private static boolean isValid(String str) {
    Pattern pattern = Pattern.compile("^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$", Pattern.CASE_INSENSITIVE);
    Matcher emailAddress = pattern.matcher(str);
    return emailAddress.matches();
}

FUNCTION 2

private static boolean isValid(String str) {
    Pattern pattern = Pattern.compile("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,6}$", Pattern.CASE_INSENSITIVE);
    Matcher emailAddress = pattern.matcher(str);
    return emailAddress.matches();
}

I have tried to do some research on regex on here and with some books and it seems like the concept of regex is not necessarily exact when it comes to validating email addresses. The hope is to validate ALMOST all email address. However, CodeEval seems to think otherwise. I tried using the regex from HERE but their online editor/compilers complained of 100+ errors with that regex. My question is, is there some other regex that surely validate EVERY single email under the sun? Thank you in advance for any comment(s)/answer(s)

sparrow
  • 450
  • 5
  • 13

0 Answers0