0

I am using a string which contains a regex expression to validate an email, the email is validating properly,unless a "+" symbol is included in the email. if someone has a email id suppose like this "billgates+apple@gmail.com", so at this point my regex is not working itz giving me a invalid username message,for else email id itz working fine here is my regex

 string strPattern = "^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$";

I tried using other regex which validates for special symbols but still i have the same error.

 string strPattern = "^[a-zA-Z0-9]+([a-zA-Z0-9_.\\$&-]+)*@[a-zA-Z0-9]+([a-zA-Z0-9_.\\$&-]+)*\\.([a-zA-Z0-9]{2,4})$";

if anyone can help out,it will be usefull,thanx

Safwan
  • 171
  • 1
  • 17

1 Answers1

0

Using something like this should work: ^([0-9a-zA-Z]([-.\\w\\+]*[0-9a-zA-Z\\+])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$.

This assumes that you can use the plus sign in the name, but not the domain. The email cannot start with a plus sign either. If it is made from one character, then, that character cannot be a plus either.

This code:

        string[] terms = new string[] {"bob@bobmail.com", "+@mail.com", "+bob@mail.com", "bob1+bob2@email.com", "bob1+@microsoft.com" };
        Regex regex = new Regex("^([0-9a-zA-Z]([-.\\w\\+]*[0-9a-zA-Z\\+])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$");
        foreach (string term in terms)
        {
            System.Console.WriteLine(regex.IsMatch(term));
        }
        System.Console.ReadKey();

Yields:

True
False
False
True
True

I do not know if you wish the result you are getting for the last case (bob1+@microsoft.com).

npinti
  • 51,780
  • 5
  • 72
  • 96
  • it is also not working. – Safwan Jun 09 '14 at 10:11
  • This should be working. Can you give us a few examples of your input string? – Stonehead Jun 09 '14 at 11:21
  • @safwan: Please check [this](http://regex101.com/r/cA3wF1) link, it is working for me. – npinti Jun 09 '14 at 11:32
  • is it language specific???because i m using c# – Safwan Jun 09 '14 at 11:52
  • string strPattern = "^[a-zA-Z0-9]+([a-zA-Z0-9_.\\$&-]+)*@[a-zA-Z0-9]+([a-zA-Z0-9_.\\$&-]+)*\\.([a-zA-Z0-9]{2,4})$"; – Safwan Jun 09 '14 at 11:52
  • string strPattern = @"^[a-z][a-z|0-9|]*([_][a-z|0-9]+)*([.][a-z|0-9]+([_][a-z|0-9]+)*)?@[a-z][a-z|0-9|]*\.([a-z][a-z|0-9]*(\.[a-z][a-z|0-9]*)?)$"; – Safwan Jun 09 '14 at 11:53
  • @safwan: If you copy/paste the regex in my answer it should work. What you are using now does not seem to be the same that you are saying that you are using in your question. – npinti Jun 09 '14 at 11:55
  • @npinti i copy/paste your regex only,and tried it lots of time,bt showing me the same thing. – Safwan Jun 09 '14 at 11:59