0

I've come across a description of a decent email regex.

/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/

But I would also like to include some punctuation characters into the first part of the email whilst retaining the rest of the functionality (such as no repeated .)

The best I have come up with so far is:

([\/!#$%&'*+-=?^_`{|}~]*\w+)([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,40})+$

But this will not allow punctuation at the end of the first part of the email.

I find regex so confusing, does anyone know how to properly implement this?

Example:

Yes email@domain.com
Yes firstname.lastname@domain.com
Yes email@subdomain.domain.com
Yes firstname+lastname@domain.com
Yes email@123.123.123.123
Yes email@[123.123.123.123]
Yes "email"@domain.com
Yes 1234567890@domain.com
Yes email@domain-one.com
Yes _______@domain.com
Yes email@domain.name
Yes email@domain.co.jp
Yes firstname-lastname@domain.com
No  plainaddress
No  #@%^%#$@#$@#.com
No  @domain.com
No  Joe Smith <email@domain.com>
No  email@domain@domain.com
No  .email@domain.com
No  email.@domain.com
No  email..email@domain.com
No  あいうえお@domain.com
No  email@domain.com (Joe Smith)
No  email@domain
No  email@-domain.com
No  email@domain.web
RonnyKnoxville
  • 6,166
  • 10
  • 46
  • 75
  • Can you provide sample inputs? – anubhava Dec 21 '15 at 11:39
  • 1
    Best tip I can give is: start small (just the basics) with regex and expand from there. This keeps you in control of what you're doing instead of trial error like approach ☺ – 321X Dec 21 '15 at 11:40
  • 1
    Take a look at this thread [Validate email address in javascript](http://stackoverflow.com/questions/46155/validate-email-address-in-javascript) – Klante Dec 21 '15 at 11:40
  • Do you want generic email validation regexp? If so, this question duplicate - see link in comment above. Or do you want to allow only some specific emails? – Oleg V. Volkov Dec 21 '15 at 13:17
  • No I've got a specific type of regex in mind. There are a lot of different email regex but this is one to fit a specific set of requirements. – RonnyKnoxville Dec 21 '15 at 14:05
  • Why is `email@domain.web` invalid? – anubhava Dec 21 '15 at 14:27
  • Is there a reason why my answer does not fulfill your requirements? – BenG Dec 21 '15 at 14:35

3 Answers3

1

This is my closest attempt now, I think it is roughly correct

^[a-zA-Z0-9_!#$%&‘*\=\+\/\?^{|}~]+([\.-]?[a-zA-Z0-9_!#$%&‘*\=\+\/\?^{|}~]+)*@\w+([\.-]?\w+)*(\.\w{2,50})+$
RonnyKnoxville
  • 6,166
  • 10
  • 46
  • 75
0

You need to replace \w with its equivalent [A-Za-z0-9_] and escape all the special characters from here.

try this:-

/^[\/\!#\$\%&'\*\+\-\=\?\^_`\{\|\}~A-Za-z0-9]+[\.-]?[\/\!#\$\%&'\*\+\-\=\?\^_`\{\|\}~A-Za-z0-9]*@\w+([\.-]?\w+)*(\.\w{2,40})+$/
Community
  • 1
  • 1
BenG
  • 14,826
  • 5
  • 45
  • 60
0
([a-zA-Z0-9\-\_\"]+)([\_\.\-\+{1}])?([a-zA-Z0-9\-\"\_]+)\@([a-zA-Z0-9\[]+)([\-])?([a-zA-Z0-9]+)?([\.])([a-zA-Z0-9\.\]]+)

Jack, the expression I have mentioned above will provide the solution for this problem. This will give the desired results according to the question.

Yogesh Chauhan
  • 348
  • 4
  • 11