using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^\w+(?:[_-][^\W_]+)*@(?![\d._-]+\.[^\W_]+$)[^\W_]+(?:[_-][^\W_]+)*\.[^\W_]{2,3}$";
string input = @"xxx@_domain.com
xxx@domain_.com
xxx@-domain.com
xxx@domain-.com
-xxx@domain.in
xxx@111.com
xxx@1and1.com
xxx@us.com
xxx@my-india.com
xxx@me2.com
xxx@tv.com
xxx@abc_efg.com
_muth@tamil.com
xx-u@my-india.commmmm
x-xx@1and1.com
xxx@_domain.com => invalid @ starts with _
xxx@domain_.com => invalid domain ends with _
xxx@-domain.com => invalid @ starts with - ( hypen)
xxx@domain-.com => invlaid domain ends with - (hypen)
-xxx@domain.in => invalid domain start with - (hypen)
xxx@111.com => invalid domain only contains numbers
xxx@1and1.com => valid domain contains both number and chars
xxx@us.com => valid
xxx@my-india.com => valid
xxx@me2.com => valid
xxx@tv.com =>
xxx@abc_efg.com => valid _ inside the chars
_muth@tamil.com => valid _ start with ";
RegexOptions options = RegexOptions.Multiline;
foreach (Match m in Regex.Matches(input, pattern, options))
{
Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
}
}
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for C#, please visit: https://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex(v=vs.110).aspx