简体   繁体   English

此电子邮件验证表达式在Java中如何工作?

[英]How this Email Validation Expression works in Java?

I am using this method to validate Email in Java. 我正在使用此方法来验证Java中的电子邮件。 I want to understand it. 我想了解。 Can someone explain what this Expression exclude and include 有人可以解释此表达式排除和包括哪些内容

String expression = [A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4};

Below is full method : 下面是完整的方法:

public static boolean isValid(String email)
{
   //String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
   String expression = "[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}";
   //String expression = "^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";
   CharSequence inputStr = email;
   Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
   Matcher matcher = pattern.matcher(inputStr);
   if (matcher.matches()) 
   {
      return true;
   }
   else{
   return false;
   }
}

Simranjeet is mostly correct. Simranjeet通常正确的。 The regex [AZ]+ maps to one or more UPPERCASE letters. 正则表达式[AZ] +映射到一个或多个大写字母。 The reason the regex you've given works for all letters (even lowercase) is that Pattern.CASE_INSENSITIVE ensures upper/lowercase compatibility, 您提供的正则表达式适用于所有字母(甚至小写)的原因是Pattern.CASE_INSENSITIVE确保大写/小写兼容性,

  • [A-Z0-9._%+-]+ the first part of mail address may contain all characters, numbers, points, underscores, percent, plus and minus. [A-Z0-9 ._%+-] +邮件地址的第一部分可能包含所有字符,数字,点,下划线,百分比,正负。

  • @ the @ character is mandatory @ @字符是强制性的

  • [A-Z0-9.-]+ the second part of mail address may contain all characters, numbers, points, underscores. [A-Z0-9 .-] +邮件地址的第二部分可能包含所有字符,数字,点,下划线。
  • \\. \\。 the point is mandatory 这一点是强制性的
  • [AZ]{2,4} the domain name may contain all characters. [AZ] {2,4}域名可能包含所有字符。 The number of characters is limited between 2 and 4. 字符数限制在2到4之间。

Refer this link " http://www.sw-engineering-candies.com/blog-1/howtofindvalidemailaddresswitharegularexpressionregexinjava " 请参阅此链接“ http://www.sw-engineering-candies.com/blog-1/howtofindvalidemailaddresswitharegularexpressionregexinjava

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM