Regex for two consecutive dot email domain failure

I am trying to verify the domain part of an email message, and I want to verify that there are no two consecutive points in the domain, that is, the following are not valid.

  • joe @ ms..com
  • joe @ ms.com..uk
  • joe @ .. ms.com
  • joe@ms.com.au..

I have a regex to find it

@+\.{2}|@.+\.{2} 

but I use the Regex attribute in .NET and want to build a valid regular expression (i.e. a single number), and not the one that fails

I thought the ^ symbol means

all that is NOT an expression

so I thought ^(@+\.{2}|@.+\.{2}) would work, but it doesn’t support the .NET Framework EmailAddress Attribute - it stops joe@ms..com but does not stop joe@ms.com..au

+1
source share
1 answer

You can use negative lookup before your regex:

 @(?!.*?\.\.)[^@]+$ 

That will not coincide if there are 2 consecutive points in the domain somewhere in the domain.

[^@] will match 1 or more characters that are not @

RegEx Demo

+1
source

Source: https://habr.com/ru/post/985759/


All Articles