简体   繁体   中英

How this Email Validation Expression works in Java?

I am using this method to validate Email in 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. The regex [AZ]+ maps to one or more UPPERCASE letters. The reason the regex you've given works for all letters (even lowercase) is that Pattern.CASE_INSENSITIVE ensures upper/lowercase compatibility,

  • [A-Z0-9._%+-]+ the first part of mail address may contain all characters, numbers, points, underscores, percent, plus and minus.

  • @ the @ character is mandatory

  • [A-Z0-9.-]+ the second part of mail address may contain all characters, numbers, points, underscores.
  • \\. the point is mandatory
  • [AZ]{2,4} the domain name may contain all characters. The number of characters is limited between 2 and 4.

Refer this link " http://www.sw-engineering-candies.com/blog-1/howtofindvalidemailaddresswitharegularexpressionregexinjava "

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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