0

I have tried to find a solution to my problem here and here but without luck.
I have written these 2 RegExes for email validation which pretty meet almost all of my criteria.

RegEx 1

^\w?[A-Za-z]{1}\w?@[a-z]+\.[a-z]{2,}(\.[a-z]{2,}){0,1}$

RegEx 2

[\w\.\-]+@[a-z]+\.[a-z]{2,}(\.[a-z]{2,}){0,1}$

But they do not solve 1 critical issue.
I want the RegEx to fail when matched with something like:
_@gmail.com
_@gmail.com
8
@gmail.com
8@gmail.com
88@gmail.com
8
@gmail.com

So basically I want the email to have at least 1 lower or upper case letter and it does not matter it is at the beginning , middle or end of the email itself(before the @).

Could you please help me with this?

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
Eric Movsessian
  • 488
  • 1
  • 11

2 Answers2

2

You might use

^[^\s@]*[A-Za-z][^\s@]*@[^\s@]+\.[a-z]{2,}$

Explanation

  • ^ Start of string
  • [^\s@]* Match optional chars other than @ or a whitespace char
  • [A-Za-z]
  • [^\s@]* Match optional chars other than @ or a whitespace char
  • @ Match literally
  • [^\s@]+ Match 1+ chars other than @ or a whitespace char
  • \.[a-z]{2,} Match . and 2 or more chars a-z
  • $ End of string

See a .NET regex101 demo.

The fourth bird
  • 154,723
  • 16
  • 55
  • 70
1

You could use positive look ahead to do this.
(?=.*[a-z]) for lowercase
(?=.*[A-Z]) for uppercase

So something like this should do the trick:

(?=.*[a-z])(?=.*[A-Z])[^@ \t\r\n]+@[^@ \t\r\n]+\.[^@ \t\r\n]+

I let you customize the filter after the @ as you seem to have specific needs.

bobble bubble
  • 16,888
  • 3
  • 27
  • 46
pgossa
  • 171
  • 9