0

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;
   }
}
user2568219
  • 95
  • 3
  • 11
  • Have you already gone through some [Regular Expression basics](http://web.mit.edu/hackl/www/lab/turkshop/slides/regex-cheatsheet.pdf)? In addition to that, you might be interested in this: http://stackoverflow.com/questions/201323/using-a-regular-expression-to-validate-an-email-address?rq=1 – reto Jul 19 '13 at 06:41
  • Thanks reto...That's helpful – user2568219 Jul 19 '13 at 06:54

2 Answers2

3

Simranjeet is mostly correct. The regex [A-Z]+ 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,

lf215
  • 1,185
  • 7
  • 41
  • 83
1
  • [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
  • [A-Z]{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"

  • Simranjeet - Thanks a lot. When you say characters. Does it also contains Special Characters ? or only A-Z letters ? – user2568219 Jul 19 '13 at 06:54
  • [A-Z0-9._%+-]+ It means only the characters that are mentioned in this regex expression. To include any other special characters, you will have to make them a part of the regex expression. So, if you want to allow parentheses in the email then you will have to mention them in the regex. – Simranjeet Singh Chawla Jul 19 '13 at 07:09
  • Thanks i got it....I came accross one valid email case "example@example-abcd.com" but this RegEx = "[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}" won't approve. Any idea why it won't work, because i already see "-" here "@[A-Z0-9.-]" – user2568219 Jul 19 '13 at 09:02