Regular expression for Email Validation in java with example (top level domain and subdomain)

Before learning how to write regular expression for email validation in java , we should understand the term email address . An email address identifies an email box to which email messages are delivered.There is a good site to  test , regular expression validator email , regexpal .

An email addresses are not only used in mail clients or on a mail servers , but also used in the websites where a user supplied email address is often validated. Assuring an email address is of  a good quality  requires a combination of various validation techniques.



Regular Expression Validator Email :


"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+
(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$"


Explanation


 ^                     * start of the line
   [_A-Za-z0-9-]+      * expression inside[ ] indicates string can only contain alphabets,digits
                         (+)  must contains one or more
   (                   * start of group 1
    \\.[_A-Za-z0-9-]+  * must be followed by a dot "." and string in the bracket [ ],
                         (+)  must contains one or more
   )*                  * end of group 1, (*) indicates that this group is optional
   @                   * must contains "@" symbol
   [A-Za-z0-9-]+       * follow by [] a string that can only contain alphabets,digits
                         (+)  must contains one or more
  (                    * start of group 2 - first level : Top Level Domain checking
   \\.[A-Za-z0-9-]+    * follow by a dot "." and string in the bracket [ ], must contains one or more (+)
  )*                   * end of group 2, this group is optional (*)
  (                    * start of group 3 - second level :Top Level Domain checking
   \\.[A-Za-z]{2,}     * follow by a dot "." and [] a string that can only contain alphabets with minimum
                         length of 2
  )                    * end of group 3
$                      * end of the line


The backslash  \ is a predefined meaning in java . It is known as the escape character . \\ is an escape telling Java that you do not want to use.If you want to define \w, then you must be using \\w in your regex.


Code :




import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class EmailValidator {
    
    public static void main(String[] args) {
        try {
            String mydomain = "javahungry@blogspot.com";
            String emailregex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*(\\.[A-Za-z]{2,})$";
            Boolean b = mydomain.matches(emailregex);
            
            if (b == false) {
                System.out.println("Email Address is Invalid");
                }else if(b == true){
                System.out.println("Email Address is Valid");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
}

Valid for the email ids which contain alphabets, digits and - (hyphen) before @ symbol. Following are                        

java hungry email validation regular expression icon
       Valid email ids  according to the above regular expression :

        javahungry@oracle.com              
        javahungry9@abc.com
        javahungry@gmail.com
        javahungry@123456.com
        javahungry-9@gmail.com
        javahungry.9@gmail.com
        javahungry@javahungry.com





TOP LEVEL DOMAIN VALIDATION


Next Question :  What will be the regular expression if we also need to validate top level domain  
                             (tld)  names   

Let us understand the question first . If we want to restrict user to enter only specific top level domains like .com , .org . Only email id containing (com , org )tld are valid . Email ids containing other than these tld are invalid email ids .

for example

Valid email id         :         javahungry@blogspot.com
                                               javahungry@gmail.org

Invalid email id      :         javahungry@gmail.net
                                                 javahungry@gmail.edu
                                                 javahungry@oracle.gov


"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.(?:[A-Z]{2,}|com|org))*$"


Explanation 


^                        * start of the line
 [_A-Za-z0-9-]+          * expression inside[ ] indicates string can only contain alphabets,digits
                           (+)  must contains one or more
 (                       * start of group 1 
   \\.[_A-Za-z0-9-]+     * must be followed by a dot "." and string in the bracket [ ],
                           (+)  must contains one or more
 )*                      * end of group 1, (*) indicates that this group is optional
   @                     * must contains "@" symbol
    [A-Za-z0-9-]+        * follow by [] a string that can only contain alphabets,digits
                           (+)  must contains one or more
     (                   * start of group 2 - first level : Top Level Domain checking
       \\.[A-Z]{2,}      * follow by a dot "." and [] a string that can only contain uppercase alphabets with
                           minimum length of 2
       |com|org          * extension can only be com or org
     )*                  * end of group 2, this group is optional (*)
$                        * end of the line



Besides .com and .org ,we can use any of top level domain to validate email address by changing the above words in the red with .edu, .gov, .mobile , .java, .javahungry etc.


Code :



import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class EmailTopLevelDomainValidator {
    
    public static void main(String[] args) {
        try {
            String mydomain = "javahungry@gmail.com";
            String emailregex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.(?:[A-Z]{2,}|com|org))+$";
            Boolean result = mydomain.matches(emailregex);
            
            if (result == false) {
                System.out.println("Email Address is Invalid");
                }else if(result == true){
                System.out.println("Email Address is Valid");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
}



SUBDOMAIN VALIDATION


Following are the examples of subdomain validation with  valid email addresses
        javahungry@javahungry.com.us                                            
        javahungry@oracle.co.in
           

"^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.(?:[A-Z]{2,}|com|co)*(\\.(?:[A-Z]{2,}|us|in|ch|jp|uk)*))+$"


 
Explanation




^                          * start of the line
  [_A-Za-z0-9-]+           * expression inside[ ] indicates string can only contain alphabets,digits
                             (+)  must contains one or more
  (                        * start of group 1
    \\.[_A-Za-z0-9-]+      * must be followed by a dot "." and string in the bracket [ ],
                             (+)  must contains one or more
  )*                       * end of group 1, (*) indicates that this group is optional
    @                      * must contains "@" symbol
     [A-Za-z0-9-]+         * follow by [] a string that can only contain alphabets,digits
                             (+)  must contains one or more
     (                     * start of group 2 - first level : Top Level Domain checking
       \\.[A-Z]{2,}        * follow by a dot "." and [] a string that can only contain uppercase alphabets with
                             minimum length of 2
       |com|co             * extension can only be com or org
     )*                    * end of group 2, this group is optional (*)
    (                      * start of group 3 - second level : Top Level Domain checking
      \\.[A-Z]{2,}         * follow by a dot "." and [] a string that can only contain uppercase alphabets with
                             minimum length of 2
    |us|in|ch|jp|uk        * subdomain can only be us,in,ch,jp,uk
    )*                     * end of group 3, this group is optional (*)
$                          * end of the line



Code :


import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class EmailSubDomainValidator {
    
    public static void main(String[] args) {
        try {
            String mydomain = "javahungry@gmail.co.in";
            String emailregex = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.(?:[A-Z]{2,}|com|co)*(\\.(?:[A-Z]{2,}|us|in|ch|jp|uk)))+$"; 
            Boolean result = mydomain.matches(emailregex);
            
            if (result == false) {
                System.out.println("Email Address is Invalid");
                }else if(result == true){
                System.out.println("Email Address is Valid");
            }
        }
        catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }
    }
}


Please write in comments in case you have any queries or doubts .

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry