How to check two email addresses for equality

Is there an exhaustive way to check if two email addresses match? I know that I can hurt both. But there are other rules that differ from server to server. For example, " william.burroughs@gmail.com ", " will.iam.burroughs@gmail.com " and " williamburroughs@gmail.com " are equivalent for gmail. But I do not think that this is true in all cases. Therefore, given the two email addresses, I need to make sure that they are equivalent. My code does not currently consider " william.burroughs@gmail.com " and " williamburroughs@gmail.com " the same. I can start a special shell something like "gmail", so there are some, but I was hoping there was a better approach.

+4
source share
4 answers

There are really only two rules for setting up Gmail

  • Any periods ( .) are ignored. This is easily overcome with the regex for gmail addresses.
  • Any event after is +ignored. Again. regex will fix this for gmail addresses.

The biggest problem I see is that Google contains thousands of cloud domains that don't end with googlemail.comor gmail.com. The only way to recognize them is to do a DNS lookup and see what MX records in the domain. Here is an example python that works:

http://eran.sandler.co.il/2011/07/17/determine-if-an-email-address-is-gmail-or-hosted-gmail-google-apps-for-your-domain/

. "MX" gmail googlemail.

, Google, .

0

, , . , "([\ w \.] +) @([\ W \.] + \.\W +)". 2 , 1 , .

boolean emailsEquals(String email1,String email2) {
    Pattern address=Pattern.compile("([\\w\\.]+)@([\\w\\.]+\\.\\w+)");
    Matcher match1=address.matcher(email1);
    Matcher match2=address.matcher(email2);
    if(!match1.find() || !match2.find()) return false; //Not an e-mail address? Already false
    if(!match1.group(2).equalsIgnoreCase(match2.group(2))) return false; //Not same serve? Already false
    switch(match1.group(2).toLowerCase()) {
    case "gmail.com":
        String gmail1=match1.group(1).replace(".", "");
        String gmail2=match2.group(1).replace(".", "");
        return gmail1.equalsIgnoreCase(gmail2);
    default: return match1.group(1).equalsIgnoreCase(match2.group(1));
    }
}

, !

+1

Part of a custom coded rule set for specified email providers (e.g. gmail for your example). I don’t think there is another way ...

0
source

This works here, but maybe I didn’t understand your question

    String email1 = new String("william.burroughs@gmail.com");
    String email2 = new String("williamburroughs@gmail.com");

     Pattern emailFinder = Pattern.compile("gmail.com");
     Matcher emailmatcher = emailFinder.matcher(email1);
     Matcher emailmatcher1 = emailFinder.matcher(email2);
     if (emailmatcher.find() && emailmatcher1.find()) {

            email1 = email1.replaceAll(".","");
            email2 = email2.replaceAll(".","");

            if(email1.equals(email2)){
                System.out.println("The values match");
            }

     }
-2
source

All Articles