How to Validate an email address using Regular Expression in C# ?

You can validate an email address in C# using regular expressions which provides the developers to play around with different patterns.

Below is a code snippet that shows how you can use regular expression to validate an email address in C# .

How to Validate an email address using Regular Expression in C# ?

using System;
using System.Text.RegularExpressions;

namespace ACCode
{
    class Program
    {
        static void Main(string[] args)
        {
            var result1 = IsEmailAddressValid("[email protected]");
            var result2 = IsEmailAddressValid("[email protected]");
            Console.WriteLine(result1);
            Console.WriteLine(result2);
            Console.ReadLine();
        }
        // Method to validate email address using regular expression in C#
        public static bool IsEmailAddressValid(string emailAddress)
        {
            string EmailPattern = @"^(?!\.)(""([^""\r\\]|\\[""\r\\])*""|"
            + @"([-a-z0-9!#$%&'*+/=?^_`{|}~]|(?<!\.)\.)*)(?<!\.)"
            + @"@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]
Regex regex = new Regex(EmailPattern, RegexOptions.IgnoreCase); bool isValid = regex.IsMatch(emailAddress); return isValid; } } }
%d bloggers like this: