Blog / Tutorial
Email Regex C# Guide: Simplified Validation with Practical Examples
When dealing with email validation in your C# applications, a common approach is to use regular expressions (regex). While regex is powerful, it should not be overly complex nor too strict, as it can result in rejecting valid email addresses. This guide provides a ‘good enough’ regex for basic email format validation, practical examples, and a complementary approach using an email verification API to check the address is real.
Basic Email Regex in C#
Here’s a practical regex pattern you can use to validate email addresses in C#:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string emailRegex = @"^[^@\s]+@[^@\s]+\.[^@\s]+$";
Regex regex = new Regex(emailRegex);
string[] testEmails = {
"[email protected]",
"[email protected]",
"invalidemail@domain",
"@novalue.com",
"[email protected]",
"[email protected]",
"[email protected]"};
foreach (string email in testEmails)
{
Console.WriteLine($"{email} is {(regex.IsMatch(email) ? "valid" : "invalid")}");
}
}
}
Expected Output
[email protected] is valid
[email protected] is valid
invalidemail@domain is invalid
@novalue.com is invalid
[email protected] is valid
[email protected] is valid
[email protected] is valid
Explanation of the Regex Pattern
^[^@\s]+: Ensures the string starts with one or more characters that are not ‘@’ or whitespace.@: Requires an ‘@’ symbol.[^@\s]+: Ensures the domain name contains one or more characters that are not ‘@’ or whitespace.\.: Requires a literal dot ‘.’.[^@\s]+$: Ensures the domain extension contains one or more characters that are not ‘@’ or whitespace and the string ends there.
Examples of Valid Email Addresses
Examples of Invalid Email Addresses
invalidemail@domain(no dot in the domain)@novalue.com(nothing before the ‘@’)
What This Regex Lets Through
A simple pattern only checks the overall shape of an address, so a few addresses that no real mailbox could have still pass:
[email protected](consecutive dots)[email protected](a one-letter extension)[email protected](a domain starting with a hyphen)
That’s an acceptable trade-off for a quick format check. These addresses are rare, and none of them have a real domain behind them, so a DNS lookup on the domain will catch them.
Justification for the Regex Complexity
This ‘good enough’ regex is simple and covers most common email formats without over-complicating the validation logic. While stricter regex patterns can catch more edge cases, they may also reject valid emails. For example, a rule that only allows two- to four-letter extensions would reject addresses on newer domains like .agency or .photography.
Beyond Regex: Verify the Address
A regex only checks format. It can’t tell you whether the mailbox actually exists, whether the domain accepts every address you throw at it, or whether the address is a disposable throwaway. Fuego’s email verification API answers those questions with a real SMTP check, and includes domain intelligence about the company behind the address at no extra cost:
- Verify: a
resultofdeliverable,undeliverable,riskyorunknown, plus areasonlikeaccepted_emailorinvalid_domain - Flag risky signups:
disposable,role,freeandaccept_allflags - Fix typos: a
did_you_meansuggestion for mistakes likegmial.com - Know the company: a free
domain_insightobject with the organization’s name, industry, employee count and country
using System;
using System.Net.Http;
// Verify an email with Fuego
string token = Environment.GetEnvironmentVariable("FUEGO_KEY");
string email = "[email protected]";
string url = $"https://app.gofuego.io/api/v1/verify?email={Uri.EscapeDataString(email)}&token={Uri.EscapeDataString(token)}";
using HttpClient client = new HttpClient();
string response = await client.GetStringAsync(url);
// JSON with result, reason, disposable, domain_insight and more
Console.WriteLine(response);
That’s enough to stop bad addresses at signup, suggest a fix when someone fat-fingers their domain, and know which company your new user works for.
By combining regex and an email verification API, you’ll cover everything from basic syntax checks to knowing the address is real. Start a free trial to try it out.