0

I have written this regex to validate an email address. The rules I have to follow are:

The recipient name may be a maximum of 64 characters long and consist of:

  • Uppercase and lowercase letters in English (A-Z, a-z)
  • Digits from 0 to 9
  • Special characters
  • A special character cannot appear as the first or last character in an email address or appear consecutively two or more times

[a-zA-z0-9]{1}?![a-zA-z0-9]?[a-zA-z0-9]{1,63}@[a-zA-z0-9]+\.[a-zA-z0-9]+

I am obviously new to Regex. What I meant by this line was, the first character has to be one of the [a-zA-z0-9] then, it is optional to use any special character by negating the [a-zA-z0-9] and then again use [a-zA-z0-9] before @.

Please help me to fulfil the above expectations.

Tnx

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
abbas
  • 235
  • 1
  • 3
  • 14
  • Does this answer your question? [Java regex email](https://stackoverflow.com/questions/8204680/java-regex-email) – Miss Chanandler Bong Mar 24 '20 at 16:07
  • It is so broad since the rules he is trying to follow is different than mine. – abbas Mar 24 '20 at 16:10
  • 2
    Does this answer your question? [How to validate an email address using a regular expression?](https://stackoverflow.com/questions/201323/how-to-validate-an-email-address-using-a-regular-expression) – Ilya Lysenko Mar 24 '20 at 16:37
  • With your rules, wherever you got them, you are going to exclude seven metric tonnes of _**valid**_ email addresses including some of mine with `+` in them. An email address consists of a "local address", an `@` sign, and a domain part. The domain part can be many levels such as `biology.science.example.com`, it's not limited to two (a mistake many people make), and the "local address" part can contain Absolutely _**any**_ character, _even_ another `@` if it's all escaped properly. The first answer on Ilya's link is a good explanation. Also see https://www.regular-expressions.info/email.html – Stephen P Mar 24 '20 at 17:19
  • Another good article: [Stop Validating Email Addresses With Regex](https://davidcel.is/posts/stop-validating-email-addresses-with-regex/). If you say _why_ you are validating the email address, what is the _purpose_ of the address, we can be more helpful. For example, if the reason is that you want people to enter an email address so you can send mail to them, you _really_ don't want to exclude real people's email addresses. If this is for a web page, use the `` which already does syntax validation. The only _real_ way to validate a person's address is to send email to it. – Stephen P Mar 24 '20 at 17:28

1 Answers1

0

Try https://regex101.com/ to test regexes and read the explanation.

I think this will satisfy your requirements:

\w(?:\W(?=\w)|\w){0,63}@\w+\.\w+
  1. Matches a single word cahracter ([a-zA-Z0-9_])
  2. Match at most 63 occurences of (a single non word character immediately succeeded with a word character (i.e positive lookahead) or a single word character)
  3. Match the @ symbol
  4. Match any number of word characters, then a period and then any number of word characters again

The only problem here is the underscore, which is considered a word character, too, for some reason. If it concerns you, replace every occurrence of \w with [a-zA-Z0-9] and any occurrence of \W with [\W_] or a list of all special symbols you want to accept.

Papooch
  • 1,372
  • 11
  • 17
  • This doesn't meet the requirements. – Miss Chanandler Bong Mar 24 '20 at 17:49
  • Tnx but the regex above cannot handle emails with 2 special characters consecutively comes after one another. – abbas Mar 24 '20 at 17:53
  • @abbas oh, sorry, I misread it at first thinking it can appear at most 3 times in a row for some reason. I'll try to fix that asap. – Papooch Mar 24 '20 at 17:54
  • @abbas what about now? – Papooch Mar 24 '20 at 18:06
  • It still has some issue with consecutive characters like: "w__w@h.com" should not be accepted cause it has 2 _ one after another. I think it has a problem with _ even when it appears at the beginning. – abbas Mar 24 '20 at 18:33
  • @abbas Please read the last paragraph of my answer, where I have addressed this issue. An underscore is recognised as a word character by default. – Papooch Mar 24 '20 at 19:10
  • Special tnx, very helpful. Now, could you plz translate your regex for me? – abbas Mar 24 '20 at 19:32
  • i simply can use it but have no idea what are they for. The difference \W and [\W_] or thi part (?:\W(?=\w)|\w) – abbas Mar 24 '20 at 19:37
  • \W matches any non-word character, if I put in in square braces along with _, it matces any non-word character or a _. (?:something) is a non-capturing group (but for your usage you can omit the ?:). Google something about regexes, they are not that hard to get. The hardest part is remembering what do the different symbols stand for. You can test on the page I linked in my answer. – Papooch Mar 24 '20 at 20:51
  • Tnx again, super helpful – abbas Mar 25 '20 at 08:13
  • Can we change the Regex in order to accept w_w)w@yahoo.com ? – abbas Mar 25 '20 at 10:56
  • Having 2 characters but not consecutively – abbas Mar 25 '20 at 11:06
  • @abbas It does accept that. The rule is that there has to be etither a word cahracter or a non-word character immediately followed by a word cahracter. – Papooch Mar 25 '20 at 12:27