Email Regular Expression Pattern
^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)* @[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$;
Description
^ #start of the line [_A-Za-z0-9-\\+]+ # must start with string in the bracket [ ], must contains one or more (+) ( # start of group #1 \\.[_A-Za-z0-9-]+ # follow by a dot "." and string in the bracket [ ], must contains one or more (+) )* # end of group #1, this group is optional (*) @ # must contains a "@" symbol [A-Za-z0-9-]+ # follow by string in the bracket [ ], must contains one or more (+) ( # start of group #2 - first level TLD 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 TLD checking \\.[A-Za-z]{2,} # follow by a dot "." and string in the bracket [ ], with minimum length of 2 ) # end of group #3 $ #end of the line
The combination means, email address must start with “_A-Za-z0-9-\\+” , optional follow by “.[_A-Za-z0-9-]”, and end with a “@” symbol. The email’s domain name must start with “A-Za-z0-9-“, follow by first level Tld (.com, .net) “.[A-Za-z0-9]” and optional follow by a second level Tld (.com.au, .com.my) “\\.[A-Za-z]{2,}”, where second level Tld must start with a dot “.” and length must equal or more than 2 characters.
1. Java Regular Expression Example
Here’s a Java example to show you how to use regex to validate email address.
EmailValidator.java
package com.javaSchool.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class EmailValidator { private Pattern pattern; private Matcher matcher; private static final String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; public EmailValidator() { pattern = Pattern.compile(EMAIL_PATTERN); } /** * Validate hex with regular expression * * @param hex * hex for validation * @return true valid hex, false invalid hex */ public boolean validate(final String hex) { matcher = pattern.matcher(hex); return matcher.matches(); } }
2. Valid Emails
1. javaSchool@yahoo.com, javaSchool-100@yahoo.com, javaSchool.100@yahoo.com
2. javaSchool111@javaSchool.com, javaSchool-100@javaSchool.net, javaSchool.100@javaSchool.com.au
3. javaSchool@1.com, javaSchool@gmail.com.com
4. javaSchool+100@gmail.com, javaSchool-100@yahoo-test.com
2. javaSchool111@javaSchool.com, javaSchool-100@javaSchool.net, javaSchool.100@javaSchool.com.au
3. javaSchool@1.com, javaSchool@gmail.com.com
4. javaSchool+100@gmail.com, javaSchool-100@yahoo-test.com
3. Invalid Emails
1. javaSchool – must contains “@” symbol
2. javaSchool@.com.my – tld can not start with dot “.”
3. javaSchool123@gmail.a – “.a” is not a valid tld, last tld must contains at least two characters
4. javaSchool123@.com – tld can not start with dot “.”
5. javaSchool123@.com.com – tld can not start with dot “.”
6. .javaSchool@javaSchool.com – email’s first character can not start with dot “.”
7. javaSchool()*@gmail.com – email’s is only allow character, digit, underscore and dash
8. javaSchool@%*.com – email’s tld is only allow character and digit
9. javaSchool..2002@gmail.com – double dots “.” are not allow
10. javaSchool.@gmail.com – email’s last character can not end with dot “.”
11. javaSchool@javaSchool@gmail.com – double “@” is not allow
12. javaSchool@gmail.com.1a -email’s tld which has two characters can not contains digit
2. javaSchool@.com.my – tld can not start with dot “.”
3. javaSchool123@gmail.a – “.a” is not a valid tld, last tld must contains at least two characters
4. javaSchool123@.com – tld can not start with dot “.”
5. javaSchool123@.com.com – tld can not start with dot “.”
6. .javaSchool@javaSchool.com – email’s first character can not start with dot “.”
7. javaSchool()*@gmail.com – email’s is only allow character, digit, underscore and dash
8. javaSchool@%*.com – email’s tld is only allow character and digit
9. javaSchool..2002@gmail.com – double dots “.” are not allow
10. javaSchool.@gmail.com – email’s last character can not end with dot “.”
11. javaSchool@javaSchool@gmail.com – double “@” is not allow
12. javaSchool@gmail.com.1a -email’s tld which has two characters can not contains digit
4. Unit Test
EmailValidatorTest.java
package com.javaSchool.regex; import org.testng.Assert; import org.testng.annotations.*; /** * Email validator Testing * * @author javaSchool * */ public class EmailValidatorTest { private EmailValidator emailValidator; @BeforeClass public void initData() { emailValidator = new EmailValidator(); } @DataProvider public Object[][] ValidEmailProvider() { return new Object[][] { { new String[] { "javaSchool@yahoo.com", "javaSchool-100@yahoo.com", "javaSchool.100@yahoo.com", "javaSchool111@javaSchool.com", "javaSchool-100@javaSchool.net", "javaSchool.100@javaSchool.com.au", "javaSchool@1.com", "javaSchool@gmail.com.com", "javaSchool+100@gmail.com", "javaSchool-100@yahoo-test.com" } } }; } @DataProvider public Object[][] InvalidEmailProvider() { return new Object[][] { { new String[] { "javaSchool", "javaSchool@.com.my", "javaSchool123@gmail.a", "javaSchool123@.com", "javaSchool123@.com.com", ".javaSchool@javaSchool.com", "javaSchool()*@gmail.com", "javaSchool@%*.com", "javaSchool..2002@gmail.com", "javaSchool.@gmail.com", "javaSchool@javaSchool@gmail.com", "javaSchool@gmail.com.1a" } } }; } @Test(dataProvider = "ValidEmailProvider") public void ValidEmailTest(String[] Email) { for (String temp : Email) { boolean valid = emailValidator.validate(temp); System.out.println("Email is valid : " + temp + " , " + valid); Assert.assertEquals(valid, true); } } @Test(dataProvider = "InvalidEmailProvider", dependsOnMethods = "ValidEmailTest") public void InValidEmailTest(String[] Email) { for (String temp : Email) { boolean valid = emailValidator.validate(temp); System.out.println("Email is valid : " + temp + " , " + valid); Assert.assertEquals(valid, false); } } }
Here’s the unit test result.
Email is valid : javaSchool@yahoo.com , true Email is valid : javaSchool-100@yahoo.com , true Email is valid : javaSchool.100@yahoo.com , true Email is valid : javaSchool111@javaSchool.com , true Email is valid : javaSchool-100@javaSchool.net , true Email is valid : javaSchool.100@javaSchool.com.au , true Email is valid : javaSchool@1.com , true Email is valid : javaSchool@gmail.com.com , true Email is valid : javaSchool+100@gmail.com , true Email is valid : javaSchool-100@yahoo-test.com , true Email is valid : javaSchool , false Email is valid : javaSchool@.com.my , false Email is valid : javaSchool123@gmail.a , false Email is valid : javaSchool123@.com , false Email is valid : javaSchool123@.com.com , false Email is valid : .javaSchool@javaSchool.com , false Email is valid : javaSchool()*@gmail.com , false Email is valid : javaSchool@%*.com , false Email is valid : javaSchool..2002@gmail.com , false Email is valid : javaSchool.@gmail.com , false Email is valid : javaSchool@javaSchool@gmail.com , false Email is valid : javaSchool@gmail.com.1a , false PASSED: ValidEmailTest([Ljava.lang.String;@15f48262) PASSED: InValidEmailTest([Ljava.lang.String;@789934d4) =============================================== Default test Tests run: 2, Failures: 0, Skips: 0 ===============================================
References
Sign up here with your email
ConversionConversion EmoticonEmoticon