GoldenUser
GoldenUser

Reputation: 365

Regular Expression for valid domains

Regex(@"@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$", RegexOptions.Compiled);

Using the above, I want to pass only values like @gmail.com, @xyz.edu, @co.uk etc... But I find that the values like [email protected] (valid email ids) also pass through.

What should I modify?

Upvotes: 1

Views: 1635

Answers (2)

Ωmega
Ωmega

Reputation: 43673

Domain cannot start with - character.

^(?:[a-zA-Z0-9][a-zA-Z0-9-]*\.)+[a-zA-Z]{2,6}$


Uri uri = new Uri("http://www.cnn.com/some_link/document.htm");
string url = uri.Host.ToString();

Upvotes: 2

Jan Jongboom
Jan Jongboom

Reputation: 27322

Put a ^ in front of the @.

By the way, why the {2,4} in the end. .travel is a valid top level domain as well.

Upvotes: 3

Related Questions