Validate Email Address With Regular Expression

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.

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. mkyong@yahoo.com, mkyong-100@yahoo.com, mkyong.100@yahoo.com
  2. mkyong111@mkyong.com, mkyong-100@mkyong.net, mkyong.100@mkyong.com.au
  3. mkyong@1.com, mkyong@gmail.com.com
  4. mkyong+100@gmail.com, mkyong-100@yahoo-test.com

3. Invalid Emails

  1. mkyong – must contains “@” symbol
  2. mkyong@.com.my – tld can not start with dot “.”
  3. mkyong123@gmail.a – “.a” is not a valid tld, last tld must contains at least two characters
  4. mkyong123@.com – tld can not start with dot “.”
  5. mkyong123@.com.com – tld can not start with dot “.”
  6. .mkyong@mkyong.com – email’s first character can not start with dot “.”
  7. mkyong()*@gmail.com – email’s is only allow character, digit, underscore and dash
  8. mkyong@%*.com – email’s tld is only allow character and digit
  9. mkyong..2002@gmail.com – double dots “.” are not allow
  10. mkyong.@gmail.com – email’s last character can not end with dot “.”
  11. mkyong@mkyong@gmail.com – double “@” is not allow
  12. mkyong@gmail.com.1a -email’s tld which has two characters can not contains digit

[Reference]
http://www.mkyong.com/regular-expressions/how-to-validate-email-address-with-regular-expression/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值