0

I am trying to match the String [email protected] using the following regular expression:

    ([_A-Za-z0-9-.]+@[a-z0-9-]+[.][a-z]{2,3}[.][a-z]{2,3})

The complete regular expression is:

   ([-A-Za-z0-9._!#$%^&*|{}'~`]+@[a-z0-9_-]+[\\.][a-z]{2,3}[\\.][a-z]{2,3})|([A-Za-z0-9.!#$%^&*|{}\"~`]+@[a-z0-9_-]+[\\.][a-z]{4})|([A-Z.a-z0-9!#$%^&*|{}'~`]+@[a-z0-9_-]+[\\.][a-z]{3})|([A-Za-z0-9.!#$%^&*_-|{}'~`]+@[a-z0-9_-]+[\\.][a-z]{2})

What changes do I need to make to the regular expression in order to be able to match the pattern specified as well?

These are some of my test cases:

 [email protected]
 [email protected]
 [email protected]
 [email protected]
 [email protected]
 [email protected]
 [email protected]
 [email protected]
 [email protected]
 [email protected]
 [email protected]
 [email protected]
 [email protected]
 The most scary test case seems to be  
 [email protected]@[email protected]@gmail.info

The regular expression Iam currently using matches

    [email protected],[email protected]

and so on.If I reverse the order of the expression I am using to matching expression with fewer number of charecters after the .,it cause a reversal matching values as:

    [email protected],[email protected]

and so on.

What should I do to ensure a proper match with case 14.Should I validate using each of these groups seperately.

4

2 Answers 2

1

this should work for you:

[A-Za-z0-9!#$%&’*+-/=?^_`\.{|}~]+@([a-z0-9_-]+[\.]?)*
2
  • Upon testing it on rubular and regexplanet I find that it only recognizes the email upto the domain name.Also can I use [.] instead of [\.].One more thing,I would like to extract multiple emails from the same string and I cannot trust my source implicitly. Oct 11, 2013 at 11:56
  • I had an issue where [email protected]@youremail.com were part of the string.What should I do in such a case? Oct 11, 2013 at 12:54
0

The regular expressions in the function below matches [email protected] while preventing invalid emails. For example it prevents adjacent periods in email address like [email protected] which is invalid according to RFC 822.

public static boolean isValidEmail(String email) {
    return email.matches("\\A[a-z0-9]+([-._][a-z0-9]+)*@([a-z0-9]+(-[a-z0-9]+)*\\.)+[a-z]{2,4}\\z")
        && email.matches("^(?=.{1,64}@.{4,64}$)(?=.{6,100}$).*");
}

See validate email address using regular expression in Java.

3
  • Please check out case 14.The possibility of the occurance of this case is not trivial.Can I modify the expression I am using in order to match case 14 because my string is quite large and very unruly.I wish I was just matching email like you did. Oct 15, 2013 at 10:14
  • Also,I know gov.in is valid,so is it [\.][a-z]{2,3}[\.][a-z]{2} that is valid. Oct 15, 2013 at 10:17
  • With case 14, you need some kind of separator between email addresses, otherwise it's almost impossible to properly match the string. The regular expression I've provided should be able to validate gov.in
    – Geek
    Oct 19, 2013 at 22:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.