using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern = @"^(?![\W_])((?:([\w-]{2,})\.?){1,})(?<![\W_])@(?![\W_])(?=[\w.-]{5,})(?=.+\..+)((?:([\w-]{2,})\.?){1,})(?<![\W_])$";
string input = @"# Valid
test@gm.com
test@gmail.com
aa-test@gmail.com
aa_test@gmail.com
aa.test@gmail.com
aa1test@gmail.com
aa1.test@gmail.com
aa1-test@gmail.com
1test@gmail.com
test@gmail.co.in
aa-test@gmail.co.in
aa_test@gmail.co.in
aa.test@gmail.co.in
aa1test@gmail.co.in
aa1.test@gmail.co.in
aa1-test@gmail.co.in
1test@gmail.co.in
# Invalid
test@gmail
.test@gmail.com
-test@gmail.com
_test@gmail.com
tes.t@gmail.com
t.e.st@gmail.com
t.-.est@gmail.com
.test.@gmail.com
test.@gmail.com
test.@.gmail.com
test@.gmail.com
test@.gmail.com.
test@gmail.com.";
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