
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!
Getting Started with C# Regular Expressions
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:
- Regex: The main class for pattern matching operations
- Match: Represents a single match result
- MatchCollection: Stores multiple match results
- Group: Represents a captured group within a match
- GroupCollection: Contains multiple Group objects
- RegexOptions: An enum with options to modify regex behavior
The RegexOptions
enum gives you tremendous flexibility in how your regex patterns work. Some commonly used options include:
- IgnoreCase: Makes pattern matching case-insensitive
- Multiline: Changes how
^
and$
match at line breaks - Compiled: Compiles the regex to an assembly for faster execution
- RightToLeft: Searches from right to left instead of the default left to right
- ExplicitCapture: Only captures groups with explicit names or numbers
Using the C# Regex Class
The Regex class is where all the regex magic happens. You have two primary ways to use it:
Method 1: Instantiate a Regex Object
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.
Method 2: Use Static Methods
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)
Finding Multiple Matches with MatchCollection
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)
Working with Groups and Captures
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)
Real-World C# Regex Example: Extracting Time Values
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)
Search and Replace with Regular Expressions
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)
Performance Tips for C# Regular Expressions
Regular expressions are powerful, but can sometimes impact performance. Here are some tips to make your regex operations faster:
- Compile patterns you use frequently: Use
RegexOptions.Compiled
when you’re creating a pattern you’ll use many times.
Regex compiledRegex = new Regex(pattern, RegexOptions.Compiled);
Code language: JavaScript (javascript)
- Use static regex instances: For patterns you use throughout your application, create static Regex instances.
private static readonly Regex EmailRegex = new Regex(@"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
Code language: PHP (php)
- Use the
Match
method instead ofIsMatch
when you need the match details: If you need both validation and extraction, useMatch
directly. - Anchor your patterns: Use
^
and$
when matching entire strings to avoid unnecessary backtracking. - Be specific: Make your patterns as specific as possible to avoid excessive backtracking.
Common C# Regex Patterns for Practical Use
Here are some regex patterns for common validation tasks in C#:
Email Validation
string emailPattern = @"^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$";
Code language: JavaScript (javascript)
Phone Number (US Format)
string phonePattern = @"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$";
Code language: JavaScript (javascript)
URL Validation
string urlPattern = @"^(https?://)?(www\.)?([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}(/[^\s]*)?$";
Code language: JavaScript (javascript)
Strong Password Validation
string passwordPattern = @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\da-zA-Z]).{8,}$";
Code language: JavaScript (javascript)
Debugging C# Regular Expression
Debugging complex regex patterns can be challenging. Here are some tips:
- Use online regex testers: Websites like regex101.com or regexr.com let you test and visualize your patterns.
- Break complex patterns into smaller parts: Test each component separately before combining them.
- Use verbose mode for complex patterns: The
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)
What’s New in Modern .NET Regex
If you’re working with .NET 7 or later, you’ll benefit from significant regex performance improvements and new features:
- Source Generators for Regex: This new feature can dramatically improve performance by generating specialized code at compile time.
- Improved timeout handling: Better control over regex execution time limits.
- RegexGenerator attribute: Allows compile-time regex validation and optimization.
// 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)
Conclusion
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!
Additional Resources
- Regular Expression Performance Best Practices
- Microsoft’s Regular Expression Language – Quick Reference
- .NET Regular Expression Design Time Compilation
Discover more from CodeSamplez.com
Subscribe to get the latest posts sent to your email.
Leave a Reply