0

I am trying to match the String m-y_n.ame@myemail.edu.in 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:

 1.vamsi$deepak0@gmail.com
 2.v_a_m_s_i_d_e_e_p_a_k_0_3@gmail.com
 3.v-a-m_si.deepak@gmail.com
 4.vamsi$deepak03@gmail.co.in
 5.v_a_m_s_i_d_e_e_p_a_k_0_3@gmail.co.in
 6.v-a-m_si.deepak@gmail.co.in
 7.vamsideepask03@bits-pilani.edu
 8.vams-ideep_ask03@bits-pilani.edu
 9.vamsi-d_.eepak03@gmool.biz
 10.songs@gaana.in
 11.vamsideep_-.ak03@footfall.net
 12.jackspa_-.rrow@pirates.org
 13.nomorespaceshuttles@nasa.gov
 The most scary test case seems to be  
 14.vamsideepak@gmail.iovamsideepak@gmail.comvamsideepak0@gmail.co.invamsideepak@gmail.info

The regular expression Iam currently using matches

    vamsideepak@gmail.iova,msideepak@gmail.comv

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:

    vamsideepak@gmail.io,vamsideepak@gmail.co

and so on.

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

vamsiampolu
  • 6,328
  • 19
  • 82
  • 183
  • 2
    At max your regex should be `^[^@]+@[^@]+$`..[Stop Validating Email Addresses With Complicated Regular Expressions](http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/) – Anirudha Oct 11 '13 at 11:27
  • To shorten your regex a little you can use `\w` (written in Java as `\\w`) instead of `[_A-Za-z0-9]`. Also can email address start with `_`, `.`? – Pshemo Oct 11 '13 at 11:27
  • According to this answer it can start with a lot of special charecters: [leading-underscores-in-an-email-address](http://stackoverflow.com/questions/9200881/leading-underscores-in-an-email-address) – vamsiampolu Oct 11 '13 at 11:34
  • @Anirudh thank you for the link,my app will not send anyone a validation email and thus I have no other means of validating the email – vamsiampolu Oct 11 '13 at 11:57

2 Answers2

1

this should work for you:

[A-Za-z0-9!#$%&’*+-/=?^_`\.{|}~]+@([a-z0-9_-]+[\.]?)*
Markus
  • 1,649
  • 1
  • 22
  • 41
  • 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. – vamsiampolu Oct 11 '13 at 11:56
  • I had an issue where myname@myemail.comyourname@youremail.com were part of the string.What should I do in such a case? – vamsiampolu Oct 11 '13 at 12:54
0

The regular expressions in the function below matches myname@myemail.edu.in while preventing invalid emails. For example it prevents adjacent periods in email address like my..name@myemail.edu.in 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.

Geek
  • 413
  • 6
  • 4
  • 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. – vamsiampolu Oct 15 '13 at 10:14
  • Also,I know gov.in is valid,so is it [\.][a-z]{2,3}[\.][a-z]{2} that is valid. – vamsiampolu Oct 15 '13 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 '13 at 22:21