How do I test two email addresses for equality

Is there a comprehensive way to test if two email addresses are equal? I know that I can universally LOWER both. But there are some other rules that differ from server to server. For example "", "", and "" are all equivalent for gmail. But I don't think this is true in all cases. So given two email addresses I need to ensure they are equivalent. Currently my code does not consider "" and "" to be the same. I can start special casing something like "gmail" so they are but I was hoping there is a better approach.


Gmail only really has two rules for customization

  1. Any periods ( . ) are ignored. This is easily overcome with a regex for gmail addresses.
  2. Anything after a + is ignored. Again. a regex will fix this for gmail addresses.

The biggest challenge as I see it is that Google hosts thousands of cloud based domains that do not end in or Your only way to recognize these would be to do a DNS lookup and see what MX record the domain points to. Here's a python example that works:

You could do the same in any other language. Look at the 'MX' record for gmail or googlemail.

For any non-google domains, you can do a lowercase string compare.

If you're comparing 2 different e-mails addresses just as characters, you may want to consider using regex to split them up for comparisons. A simple enough one could work as "([\w\.]+)@([\w\.]+\.\w+)". You could run group 2 through a switch to compare group 1 appropriately, defaulting to a more general comparison.

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(! return false; //Not same serve? Already false
    switch( {
    case "":
        String".", "");
        String".", "");
        return gmail1.equalsIgnoreCase(gmail2);
    default: return;

Hope it helps!

A part from a custom coded set of rules for given email providers (e.g gmail for your example) I don't think there is any other way...

Here this works but maybe I didnt understand your question

    String email1 = new String("");
    String email2 = new String("");

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

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

                System.out.println("The values match");


Need Your Help

Using sections in CodeIgniter

php codeigniter partial-views template-engine

I'm using ASP.NET MVC3 at work, while using PHP at home. I wish to use the Sections feature of MVC3 in CodeIgniter, does it have such feature?