Сергей Бучко Сергей Бучко - 2 months ago 20
C# Question

Combining regular expressions c#

I'm trying to make simple email validation by myself. And I need to combine two patterns:

1)

@"^[A-Za-z0-9][A-Za-z0-9._-]+[A-Za-z0-9]@[A-Za-z0-9][A-Za-z0-9.-]+\.[A-Za-z0-9.-]+[A-Za-z0-9]$"
// allows only xxxx@xxx.xx and dot is not the first or last charachter of local and domain part

2)
@"^([^\.]|([^\.])\.[^\.])*$"
// there must not be two or more dots in a row.

In other words, I want to add to the first regex a condition that
asd.asd@asd.com
is true, but
asd..asd@asd.com
is false.

vks vks
Answer
^(?!.*[.][.])[A-Za-z0-9][A-Za-z0-9._-]+[A-Za-z0-9]@[A-Za-z0-9][A-Za-z0-9.-]+.[A-Za-z0-9.-]+[A-Za-z0-9]$

Just add a lookahead for the same.

See demo.

https://regex101.com/r/dR4pQ2/1