0

I have a email address filtering regex used in Java. It works for the most part except when trying to limit repeated dot's in the username section of the email address.

The regex I'm using (with escaping removed) is [a-zA-Z0-9\.\_\-]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,5}(\.[a-zA-Z]{2,5}){0,1}

This doesn't catch a bad email address like test..test@test.com. I've tried applying limiters to the class [a-zA-Z0-9\.\_\-] but that causes it to fail on valid email addresses.

Any thoughts would be greatly appreciated.

Abslen Char
  • 3,071
  • 3
  • 15
  • 29
supertorqued
  • 109
  • 1
  • 3
  • 15

1 Answers1

2

Add a negative lookahead for two dots anchored to start:

^(?!.*\.\.)[a-zA-Z0-9._-]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,5}(\.[a-zA-Z]{2,5}){0,1}

This expression (?!.*\.\.) means the following text does not contain 2 consecutive dots.

By the way, you don't need to escape most characters when they are within a character class, including the characters ._-, ie [a-zA-Z0-9\.\_\-] is the same as [a-zA-Z0-9._-] (with the caveat that a dash is a literal dash when it appears first or last).


Using lookaheads makes adding overall constraints easy and you can easily add more, for example, to require that the overall length is at least 10 chars add (?=.{10}) to the front:

^(?=.{10})(?!.*\.\.)[a-zA-Z0-9\.\_\-]+@[a-zA-Z0-9]+\.[a-zA-Z]{2,5}(\.[a-zA-Z]{2,5}){0,1}
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • Excellent thanks for helping out. I didn't think to prefix the class as you did. Thanks for the additional information as well. – supertorqued Mar 28 '18 at 20:30