0

I need to improve a regex for email validator that is current regex I use:

'/^(\w[\+-\._\w]*@[-\._\w]*\w\.[-\w]{2,63})$/'

the issue is to improve this regex so it should filter emails like this:

"n,n@nn.nn" and "nn,nn@nn.nn";

First I have added a condition

 (?!,) 

after 6th symbol so the regex become so:

'/^(\w (?!,) [\+-\._\w]*@[-\._\w]*\w\.[-\w]{2,63})$/'

this solved the first issue "n,n@nn.nn" but not helped with "nn,nn@nn.nn"

so I added a new negative condition

 [^\,] 

after 21th symbol so the regex became so:

'/^(\w (?!,) [\+-\._\w] [^\,] *@[-\._\w]*\w\.[-\w]{2,63})$/'

that solved both issues, but in such case validator fails on correct email if the first part of email (left side from @) is only one symbol like this:

"a@aa.aa"

Please, advice me how to work it out.

galba84
  • 112
  • 8
  • The solution is to add boundaties afret second positive condition {0} ```'/^(\w (?!,) [\+-\._\w] {0}[^\,] *@[-\._\w]*\w\.[-\w]{2,63})$/'``` – galba84 Jul 12 '19 at 11:11

1 Answers1

0
(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

Please refer to https://emailregex.com/ it has a very extend guide on how to do email regex in various languages.

Yftach
  • 712
  • 5
  • 15