Email regex C#

Email

The regular expressions below can be used to validate if a string is an email address and to extract email addresses from a string. This validation method however does not guarantee that the emails validated and extracted actually exist.

The basic validation

A simple C# regex to validate string against email format and catch the most obvious syntax errors:

new Regex("^\\S+@\\S+\\.\\S+$")

Test it!
This is some text inside of a div block.

True

False

Enter a text in the input above to see the result

Example code in C#:

using System.Text.RegularExpressions;
using System.Linq;
                    
public class Program
{
    public static void Main()
    {
        // Validate if a string is a email 
        Regex validateEmailRegex = new Regex("^\\S+@\\S+\\.\\S+$");
        validateEmailRegex.IsMatch("hello@uibakery.io"); // returns True
        
        // Extract emails from a string
        Regex extractEmailsRegex = new Regex("\\S+@\\S+\\.\\S+");
        extractEmailsRegex.Matches("You can reach me out at hello@uibakery.io and contact@uibakery.io")
            .Cast<Match>()
            .Select(m => m.Value) 
            .ToArray(); // returns {"hello@uibakery.io", "contact@uibakery.io"}
    }
}

The more complex email regex

This C# regular expression will match 99% of valid email addresses and will not pass validation for email addresses that have, for instance:

  • Dots in the beginning
  • Multiple dots at the end

But at the same time it will allow part after @ to be IP address.

new Regex("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$") 

Test it!
This is some text inside of a div block.

True

False

Enter a text in the input above to see the result

Example code in C#:

using System.Text.RegularExpressions;
                    
public class Program
{
    public static void Main()
    {
        // Validate if a string is a email 
        Regex validateEmailRegex = new Regex("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");
        validateEmailRegex.IsMatch("hello@uibakery.io."); // returns False
        validateEmailRegex.IsMatch("hello@192.168.0.1"); // returns True
    }
}

RFC 5322 compliant regex

This C# regular expression is compliant to RFC 5322 standard which allows for the most complete validation. Usually, you should not use it because it is an overkill. In most cases apps are not able to handle all emails that this regex allows.

new Regex("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])")

Test it!
This is some text inside of a div block.

True

False

Enter a text in the input above to see the result

Example code in C#:

using System.Text.RegularExpressions;
                    
public class Program
{
    public static void Main()
    {
        // Validate if a string is a email 
        Regex validateEmailRegex = new Regex("(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])");
        validateEmailRegex.IsMatch("hello@uibakery.io"); // returns True
    }
}

Extra information about validating email

As was stated previously, C# regex email validation can not fully guarantee that email exists and the message can be delivered. The best way how to know for sure that email is valid is to actually send an email to that address because even paid email validation services do not provide a 100% guarantee for that.

Also, Microsoft advises to use the following code to check emails:

public bool IsValid(string emailaddress)
{
    try
    {
        MailAddress m = new MailAddress(emailaddress);

        return true;
    }
    catch (FormatException)
    {
        return false;
    }
}