In this comprehensive guide, I’ll walk you through everything you need to know about C# regular expressions to supercharge your string processing and pattern-matching capabilities. If you are new to this domain, I would highly recommend going over regular expression basics first to learn the fundamentals in a language-agnostic way.
Every major programming language supports regex, but the beauty of C# is that the .NET Framework provides a unified regex engine that works consistently across all .NET languages. Without further ado, let’s dive in!
Before diving into the practical examples, you need to know that all regex functionality in C# comes from the System.Text.RegularExpressions namespace. This namespace contains several crucial classes that will become your best friends:
using System.Text.RegularExpressions;Code language: CSS (css) The primary classes you’ll work with include:
The RegexOptions enum gives you tremendous flexibility in how your regex patterns work. Some commonly used options include:
^ and $ match at line breaksThe Regex class is where all the regex magic happens. You have two primary ways to use it:
When you need to use the same pattern multiple times, create a Regex instance first:
// Create a regex pattern for email validation
Regex emailRegex = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$", RegexOptions.IgnoreCase);
// Test multiple emails
string[] emails = {
"[email protected]",
"[email protected]",
"invalid-email"
};
foreach (string email in emails)
{
Match match = emailRegex.Match(email);
Console.WriteLine($"{email}: {match.Success}");
}Code language: PHP (php) This approach is more efficient when you’re reusing the same pattern because C# only needs to compile the regex pattern once.
For one-off pattern matching, you can use the static methods:
string[] emails = {
"[email protected]",
"invalid.email.com",
"[email protected]"
};
string pattern = @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$";
foreach (string email in emails)
{
bool isValid = Regex.IsMatch(email, pattern, RegexOptions.IgnoreCase);
Console.WriteLine($"{email}: {isValid}");
}Code language: PHP (php) What if you need to extract all matches of a pattern from a string? The Matches method returns a MatchCollection containing all matches:
string text = "Contact us at [email protected] or [email protected] for support.";
string pattern = @"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}";
MatchCollection matches = Regex.Matches(text, pattern, RegexOptions.IgnoreCase);
Console.WriteLine($"Found {matches.Count} email addresses:");
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}Code language: PHP (php) One of the most powerful features of regex is the ability to capture specific parts of a match using groups. Here’s how you can extract components from an email address:
string email = "[email protected]";
string pattern = @"^([A-Z0-9._%+-]+)@([A-Z0-9.-]+)\.([A-Z]{2,})$";
Match match = Regex.Match(email, pattern, RegexOptions.IgnoreCase);
if (match.Success)
{
string username = match.Groups[1].Value;
string domain = match.Groups[2].Value;
string tld = match.Groups[3].Value;
Console.WriteLine($"Username: {username}");
Console.WriteLine($"Domain: {domain}");
Console.WriteLine($"TLD: {tld}");
}Code language: JavaScript (javascript) You can also use named groups for better readability:
string pattern = @"^(?<username>[A-Z0-9._%+-]+)@(?<domain>[A-Z0-9.-]+)\.(?<tld>[A-Z]{2,})$";
Match match = Regex.Match(email, pattern, RegexOptions.IgnoreCase);
if (match.Success)
{
Console.WriteLine($"Username: {match.Groups["username"].Value}");
Console.WriteLine($"Domain: {match.Groups["domain"].Value}");
Console.WriteLine($"TLD: {match.Groups["tld"].Value}");
}Code language: JavaScript (javascript) Let’s look at a practical example of using regex to extract time values from a string:
// Pattern to match time in format hh:mm:ss
Regex timeRegex = new Regex(@"\b([01]?\d|2[0-3]):([0-5]\d):([0-5]\d)\b");
string text = "Server logs: Error at 03:45:21, Warning at 07:12:53, Critical at 23:59:59";
MatchCollection matches = timeRegex.Matches(text);
List<string> timeValues = new List<string>();
foreach (Match match in matches)
{
timeValues.Add(match.Value);
// You can also access individual components (hours, minutes, seconds)
string hours = match.Groups[1].Value;
string minutes = match.Groups[2].Value;
string seconds = match.Groups[3].Value;
Console.WriteLine($"Found time: {hours}:{minutes}:{seconds}");
}Code language: PHP (php) C# regex is fantastic for performing complex search and replace operations. The Replace method lets you replace matches with new text:
string text = "Contact [email protected] or [email protected] for support.";
string pattern = @"[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}";
// Basic replacement
string anonymized = Regex.Replace(text, pattern, "[EMAIL REDACTED]", RegexOptions.IgnoreCase);
Console.WriteLine(anonymized);
// Advanced replacement with a MatchEvaluator delegate
string customReplacement = Regex.Replace(text, pattern, match => {
string email = match.Value;
string[] parts = email.Split('@');
return parts[0].Substring(0, 2) + "***@" + parts[1];
}, RegexOptions.IgnoreCase);
Console.WriteLine(customReplacement); // "Contact jo***@example.com or su***@company.org for support."Code language: PHP (php) Regular expressions are powerful, but can sometimes impact performance. Here are some tips to make your regex operations faster:
RegexOptions.Compiled when you’re creating a pattern you’ll use many times.Regex compiledRegex = new Regex(pattern, RegexOptions.Compiled);Code language: JavaScript (javascript) private static readonly Regex EmailRegex = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$", RegexOptions.IgnoreCase | RegexOptions.Compiled);Code language: PHP (php) Match method instead of IsMatch when you need the match details: If you need both validation and extraction, use Match directly.^ and $ when matching entire strings to avoid unnecessary backtracking.Here are some regex patterns for common validation tasks in C#:
string emailPattern = @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$";Code language: JavaScript (javascript) string phonePattern = @"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$";Code language: JavaScript (javascript) string urlPattern = @"^(https?://)?(www\.)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(/[^\s]*)?$";Code language: JavaScript (javascript) string passwordPattern = @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,}$";Code language: JavaScript (javascript) Debugging complex regex patterns can be challenging. Here are some tips:
RegexOptions.IgnorePatternWhitespace option allows you to format your regex with whitespace and comments for readability.string verbosePattern = @"
^ # Start of string
(?=.*[a-z]) # Must contain lowercase
(?=.*[A-Z]) # Must contain uppercase
(?=.*\d) # Must contain digit
(?=.*[^\da-zA-Z]) # Must contain special char
.{8,} # At least 8 chars long
$ # End of string
";
Regex passwordRegex = new Regex(verbosePattern, RegexOptions.IgnorePatternWhitespace);Code language: PHP (php) If you’re working with .NET 7 or later, you’ll benefit from significant regex performance improvements and new features:
// In .NET 7+
[GeneratedRegex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$", RegexOptions.IgnoreCase)]
private static partial Regex EmailRegex();
// Usage
if (EmailRegex().IsMatch(input)) { <em>/* ... */</em> }Code language: PHP (php) Regular expressions in C# are an indispensable tool for any developer working with string data. They allow you to perform complex string operations with concise, powerful syntax. While they might seem intimidating at first, mastering regex will dramatically improve your ability to process and validate text data.
Remember, the key to becoming proficient with C# regex is practice. Start with simple patterns and gradually work your way up to more complex ones. Use the provided examples as a starting point, and soon you’ll be writing sophisticated regex patterns to tackle any string matching challenge.
For complete documentation, always refer to Microsoft’s official .NET Regular Expressions documentation, which contains detailed explanations and additional examples.
Now go forth and conquer string processing with C# regular expressions!
Ever wondered what happens when you run Python code? The Python runtime environment—comprising the interpreter, virtual machine, and system resources—executes your code through bytecode compilation…
Tired of repetitive tasks eating up your time? Python can help you automate the boring stuff — from organizing files to scraping websites and sending…
Learn python file handling from scratch! This comprehensive guide walks you through reading, writing, and managing files in Python with real-world examples, troubleshooting tips, and…
This website uses cookies.