Guides
Last Updated Aug 02, 2023

C# Email Regex Guide

Table of Contents:

Get your free
API
key now
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required
Get your free
Email Verification API
key now
4.8 from 1,863 votes
See why the best developers build on Abstract
START FOR FREE
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required

C# is one of the most popular modern programming languages. Companies use the language to build web applications, enterprise systems, Windows applications, games, and many more. The language, combined with the dot net framework, can access comprehensive features developed by Mircosoft, third-party companies, and developers. Among many features, C# can compile regular expressions and match the defined patterns. Regex is a general language that can be used in other languages as well. It is considered a must-have feature when you choose a programming language.

When it comes to the use cases of regex, the sky is the limit. You can find certain patterns out of a large plain text or you can validate user inputs. One of the most popular use cases is related to email. In C#, using email regular expressions, you can find or validate email addresses quickly and easily.

Let’s send your first free
API
Email Verification API
call
See why the best developers build on Abstract
Get your free api

Email regex usage

It is important to learn the context before jumping into C# regular expression. It can help you to broaden your scope of using email regex. Let's see when we would want to use the email regex.

Email format validation

C# powers countless web applications around the world. When you first visit a website and want to use the service, it is common that it requires you to join the website. One of the details you have to provide is your email address. To businesses, your email address is an important detail for communication and marketing purposes. If a company fails to collect valid email addresses, it loses a way to reach out to that customer. Furthermore, there is a chance that hackers and scammers use an invalid email address to join and explore a website to find a way to infiltrate the system.

Valid email patterns

The regular expression in C# can be used to validate email addresses. When it comes to a valid email format, there are international standards. The rules can be explained below.

  • Alphabet letters, numbers, and specific special characters including underscores, periods, and dashes
  • Underscores, periods, and dashes must be followed by one or more letters or numbers.

Let's have a look at some examples of valid emails.

  • example@email.com
  • example.abc@email.com
  • example_abc@email.com

Some of the invalid email addresses look like the following.

  • example-@email.com
  • example..abc@email.com
  • .example@email.com
  • example#example@email.com

All these rules can be interpreted in a regular expression. Since the rules are universal and can be defined in a regex which is a general language, once you define it, you can use it anywhere you need to check the validity of email addresses.

Email extraction out of unstructured text string

Regex is not only used in web applications to validate email but can also be used in the data domain. Data teams often have to handle unstructured data such as plain text. In large free text, it can contain important information such as email addresses. To extract email addresses, you can use a regular expression to detect and extract them. After extraction, you can then store email addresses in structured storage such as a relational database or semi-structured format in CSV or JSON.

Personal information protection

Identity theft is an important topic in data security. As enterprises deal with ever-growing data, it becomes a good practice to de-personalize customer details to prevent personal data from being used illegally. Among many types of personal details, email addresses can be extremely useful to hackers since they can be used as usernames for other online services.

If your customers' email addresses are mixed in a long string or plain text, you can use a regular expression to find and replace them so that you can either remove all email addresses or switch them to de-personalized email addresses. This makes sure that even if there is a data breach, the infiltrator cannot get real personal information.

Prefix split for username creation or duplication check

A valid email address has a prefix and a domain part and the two sections are split by the symbol, "@". When you sign up for a service using your email, some companies use the prefix as a username. When you log into the service, the company uses that username and displays it under your profile.

Another use case is duplication check. To attract more customers, companies often provide a free trial for a certain period. To take advantage of the free trial, some people could create more email addresses using the same prefix but different domains. By splitting an email address by the symbol and comparing the prefix with historical records, you can identify the same person.

Domain validation to block suspicious activity

Similarly, you can use the same split strategy to get the domain name and validate it if it is a trustworthy domain. If you do not check the domain that a user enters, you can potentially let users put in a random email address before carrying out suspicious activities. To avoid any risk, it is essential to stop at an early stage, and, to do so, using regex to split and grab the domain address is an important step for validation.

Email regex in C#

We learned when we want to use email regex and how it can help in the five scenarios. Let's now learn the actual implementation part of regex in C#.

Email format validation

In C#, you can access the Regex functions under the System.Text.RegularExpressions namespace. The sample code below shows you how you can validate an email address.


// Online C# Editor for free
// Write, Edit and Run your C# code using C# Online Compiler

using System;
using System.Text.RegularExpressions;  

public class RegexTest
{    
    public static void Main(string[] args)
    {
        // Email pattern in regex
        string emailRegex = @"^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$";
        
        // User email address
        string emailInput = "example@email.com";
        
        // If it's a valid email        
        if (Regex.Match(emailInput, emailRegex).Success) 
        {
            Console.WriteLine ("This is a valid email");
        }  
        // If it's not
        else 
        {
            Console.WriteLine ("This is an invalid email");
        }
    }
}

If an email input from a user is valid, it will print out "This is a valid email" since the Regex.Match() function returns success. If not, it will go to the else condition. If you use the match function, it returns a matched string. For example, if you print out the following code snippet, it will return "example@email.com".


# Returns a matched string
Console.WriteLine (Regex.Match(emailInput, emailRegex));

If you want to try an invalid email, switch the email variable to the following and execute the main class.


# Invalid email
string emailInput = "example-@email.com";

When it is not matched, the Regex.Match() function does not return anything.


# Returns null when it is not matched.
Console.WriteLine (Regex.Match(emailInput, emailRegex));

Email extraction out of unstructured text string

Data engineers and software developers sometimes have to handle unstructured data. There can be a situation where you have to extract email addresses from plain text and store extracted emails in a nice clean way.


using System;
using System.Text.RegularExpressions;  

public class RegexTest
{    
    public static void Main(string[] args)
    {
        // Email pattern in regex
        string emailRegex = @"\b[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}\b";
        
        // Plain text
        string plainText = "Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae example1@email.com vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur example2@email.com aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et example3@email.com dolore magnam aliquam quaerat voluptatem.";
        
        // Get all matches  
        MatchCollection matchedEmails = Regex.Matches(plainText, emailRegex, RegexOptions.IgnoreCase); 
        
        // Print all matched authors  
        for (int count = 0; count < matchedEmails.Count; count++) 
        {
            Console.WriteLine(matchedEmails[count]);
        }
	// It prints out
	// example1@email.com
 	// example2@email.com
	// example3@email.com
    }
}

The sample code above prints out three hidden email addresses in the text. Note that in the Regex.Matches() function used the case-insensitive option and the function returns all matched strings in a collection.

Personal information protection

There can be a case where we want to de-identify or remove personal email addresses in plain text. This can be done by the Regex.Replace() function.


Console.WriteLine(Regex.Replace(plainText, emailRegex, ""));

The plainText and emailRegex variables remain the same as the sample code above. This function first detects strings that are matched with the regex and replace them with an empty string. Using this pattern, you can replace emails with any new string you want.

Prefix split for username creation or duplication check

We can use a C# function to split an email address by the symbol, "@", and then get the string from the first index which will be the prefix.


using System;

public class RegexTest
{    
    public static void Main(string[] args)
    {
        // Email string
        string email = "detectedemail@address.com";

        // Split by @
        string[] subs = email.Split('@');

        // The first index is the prefix
        Console.WriteLine(subs[0]);
    }
}

You can use the string.Split() function to split an email. The function returns an array with each value divided by the splitting symbol.

Domain validation to block suspicious activity

Using the same function, you can extract the domain. This time, we will want to read data from index 1. You can try to change the index number from the code above as below.


Console.WriteLine(subs[1]);

Handy C# libraries for email validation

We learned the email regex for the various operations. Alternatively, if you are interested in using C# libraries for email validation, you can explore several options here.

1. System.Net.Mail

This is C# native library that allows users to validate email addresses using the class initialization. As you can see below, when you initialize the MailAddress class with an email string, it throws an exception if a format is invalid. Using the try-catch clause, we can check the validity of the input email.


using System;
using System.Net.Mail;

public class RegexTest
{    
    public static void Main(string[] args)
    {
        // Email string
        string email = "wrong@email@format.com";
        
        bool valid = true;
        
        // Check if it's valid using MailAddress
        try
        { 
            var emailAddress = new MailAddress(email);
        }
        catch
        {
            valid = false;
        }
        Console.WriteLine(valid);
    }
}

2. EmailValidation

EmailValidation is a third-party library that you can install using the following PackageReference:


<PackageReference Include="EmailValidation" Version="1.0.8" />

Or, by the Package Manager:


NuGet\Install-Package EmailValidation -Version 1.0.8

You can test it out using the following sample code.


using System;

using EmailValidation;

namespace Example {
    public class EmailTest
    {
        public static void Main ()
        {
            do {
                Console.Write ("Enter an email address: ");

                var input = Console.ReadLine ();
                if (input == null)
                    break;

                input = input.Trim ();
                Console.WriteLine ("This email, {0}, is {1}.", input, EmailValidator.Validate (input) ? "valid" : "invalid");
            } while (true);

            Console.WriteLine ();
        }
    }
}

The sample code asks for your email and returns if it is valid or not.

3. System.ComponentModel.DataAnnotations

This is a class that is supported by Microsoft. The data annotations let you apply attributes or rules to your variables to check, for example, required fields or the validity of string input.


using System.ComponentModel.DataAnnotations

public class EmailValidCheck
  {
    public static void Main(string [] args)
    {
      string invalidEmail = "testemail";
      string validEmail = "sample@domain.com";
      
      bool validEmail = ValidEmailDataAnnotations(invalidEmail);
      Console.WriteLine("The value of invalidEmail is: " + validEmail);
      
      validEmail = ValidEmailDataAnnotations(validEmail);
      Console.WriteLine("The value of validEmail is: " + validEmail);
    }

    public static bool ValidEmailDataAnnotations(string input)
    {
      return new EmailAddressAttribute().IsValid(input);
    }
  }

When you test the invalidEmail value, it will return false and the second valid email address will return true.

4. AbstractAPI Email Validation

Using C# libraries for email validation might look simple. However, the regular expression may not be able to capture all possible email addresses, and using the same validation rule across different classes and languages can be difficult. As an alternative, you can use an email validation API like AbstractAPI.

How to use AbstractAPI

  1. Go to the sign-up page and join the website.
  2. Once you finish joining, log into the website.
  3. On the main dashboard, find and click the Email validation menu.
  1. On the Email validation page, you can see Try it out section. As you can see below, it will give you the API key and sample URL address with the API key and your email address for testing.
  1. You can also check the Documentation menu to learn more details.

A request URL structure follows the patterns below.

https://emailvalidation.abstractapi.com/v1/  ? api_key = YOUR_API_KEY  & email = emailprefix@sample.com

What's interesting is that, in the response body, you can find a comprehensive analysis of the requested email. The AbstractAPI returns fields such as:

  • autocorrect: auto-corrected email address if your requested email has an incorrect value. For example, if you send sample@gmali.com, it will auto-correct it as sample@gmail.com.
  • deliverability: if a requested email is not valid, it will say "UNDELIVERABLE".
  • quality_score: it gives you an Abstract's confidence on the email address from 0.01 to 0.99.
  • is_valid_format: it tells you if it is a valid email address.
  • is_free_email: it tells you if the requested email domain is free. (e.g. Gmail, Yahoo, etc)
  • is_disposable_email: it tells you if it is a disposable email. (e.g. Mailinator, Yopmail, etc)
  • is_role_email: it tells you if it is a group email.
  • is_catchall_email: if a requested email is configured to catch all emails, it will be true.
  • is_mx_found: it returns true if MX Records for the domain can be detected.
  • is_smtp_valid: it is true if the SMTP check of the email goes through successfully.

These various types of validation checks are hard to be found in C# classes. If you try to implement them on your own, it can also be demanding. Using email validation APIs such as AbstractAPI can provide benefits and help developers to save time.

Wrapping up

We learned various ways of validating email addresses using regular expressions in C# and C# libraries. Using regex, you can perform not only email validation but also manipulate email addresses such as removing or replacing. Regardless of the email regex operations, if you get yourself familiar with regular expression patterns, it will become extremely handy for your future work. Regex is a general syntax that can be used across different programming languages and in different fields such as software development or data mining. Try the sample codes above and also try other patterns to hone your skills.

Frequently asked questions

We prepared the FAQ section to expand your regex and relevant knowledge beyond this article.

What factors do we need to consider when using regex in C#?

When you use regex in C# not just for email address validation but for other cases, one thing you need to consider is the input source. When you build a regex, knowing about the input source can help you to build an optimal and accurate regex. Is your input source constrained or not constrained? A constrained input source means that the text where you want to use a regex comes from a reliable source that follows certain rules. For example, the email input from a user registration form online is mostly constrained form. Unconstrained input means it comes from an unreliable source such as a web user who does not have to follow any rules.

What are other helpful string methods in C#?

In C#, there are multiple string methods that can help developers to handle strings.


// Assume the variable str has a string value
// Gives you the length of the string variable
int length = str.Length;

// Assume str1 and str2 are string variables
// the Concat method combines the two strings
string joined = string.Concat(str1, str2);

// the method compares the two string values
// If same, it returns true, if not, it returns false
Boolean isSame = str1.Equals(str2);

// Use the escape character to use double quotes in your string
string str = "This is the \"String\" class.";

// Checks if str1 contains str2 and returns a boolean
Boolean hasString = str1.Contains(str2);

// Convert str1 to upper or lower case
string upperString = str1.ToUpper();
string lowerString = str1.ToLower();

// Removes white spaces on the left and right
string trimmed = str1.Trim();

// Checks if str1 ends with "n"
Boolean endsWithChar = str1.EndsWith("n");

// Checks if str1 starts with "a"
Boolean startsWithChar = str1.StartsWith("a");

Where are good C# regex references?

To deepen your regex knowledge in C#, refer to the following sources.

  • Microsoft Regex Reference: the page explains available regex syntax in C# including character escapes, character classes, anchors, grouping constructs, lookarounds, quantifiers, and many more. Although you don't need to remember all of them, it would be good to know what you can do with regex in C#.
  • Regex best practices in .NET: this is another Microsoft document that explains the best practices to use regex in C#. The document covers comprehensive aspects of using regular expression in the language.
  • Regex101: this website is not a reference website, but a great online tool where you can practice your regex. You can copy and paste your text and insert your regex to test it. The website gives you a quick reference and the interpretations of your regex.

4.4/5 stars (7 votes)

Get your free
Email Verification API
API
key now
Validate & verify emails instantly using Abstract's API.
get started for free

Related Articles

Get your free
API
Email Verification API
key now
4.8 from 1,863 votes
See why the best developers build on Abstract
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
No credit card required