Regular expressions (regex) in C# provide a powerful way to match patterns in strings. They are useful for tasks such as validating input, searching for specific patterns, and manipulating strings. C# provides the System.Text.RegularExpressions
namespace, which contains classes for working with regular expressions.
Key Classes in C#
- Regex: The main class for working with regular expressions.
- Match: Represents the result of a single regular expression match.
- MatchCollection: Represents a collection of
Match
objects. - Group: Represents a capturing group in the regular expression.
- GroupCollection: Represents a collection of
Group
objects.
Basic Syntax
Here are some common regex patterns and their meanings:
Pattern | Description |
---|---|
. | Matches any single character |
^ | Matches the start of a string |
$ | Matches the end of a string |
* | Matches zero or more of the preceding element |
+ | Matches one or more of the preceding element |
? | Matches zero or one of the preceding element |
{n} | Matches exactly n occurrences of the preceding element |
{n,} | Matches n or more occurrences of the preceding element |
{n,m} | Matches between n and m occurrences of the preceding element |
[...] | Matches any one character in the brackets |
` | ` |
\ | Escapes a special character |
Example 1: Basic Regex Matching
In this example, we will use regex to check if a string is a valid email address.
csharp1using System; 2using System.Text.RegularExpressions; 3 4class Program 5{ 6 static void Main() 7 { 8 string emailPattern = @"^[^@\s]+@[^@\s]+\.[^@\s]+$"; 9 string[] testEmails = { "test@example.com", "invalid-email", "user@domain", "user@domain.com" }; 10 11 foreach (var email in testEmails) 12 { 13 if (Regex.IsMatch(email, emailPattern)) 14 { 15 Console.WriteLine($"{email} is a valid email address."); 16 } 17 else 18 { 19 Console.WriteLine($"{email} is NOT a valid email address."); 20 } 21 } 22 } 23}
Explanation
- Pattern: The regex pattern
^[^@\s]+@[^@\s]+\.[^@\s]+$
checks for a valid email format.^
: Start of the string.[^@\s]+
: One or more characters that are not@
or whitespace.@
: The@
symbol.[^@\s]+
: One or more characters that are not@
or whitespace (domain).\.
: A literal dot.[^@\s]+
: One or more characters that are not@
or whitespace (top-level domain).$
: End of the string.
Example 2: Extracting Matches
In this example, we will extract all the numbers from a string.
csharp1using System; 2using System.Text.RegularExpressions; 3 4class Program 5{ 6 static void Main() 7 { 8 string input = "There are 3 apples, 5 bananas, and 12 oranges."; 9 string numberPattern = @"\d+"; 10 11 MatchCollection matches = Regex.Matches(input, numberPattern); 12 13 Console.WriteLine("Numbers found:"); 14 foreach (Match match in matches) 15 { 16 Console.WriteLine(match.Value); 17 } 18 } 19}
Explanation
- Pattern: The regex pattern
\d+
matches one or more digits. - Regex.Matches: This method returns a collection of all matches found in the input string.
- Output: The program prints all the numbers found in the input string.
Example 3: Replacing Text
In this example, we will replace all occurrences of a specific word in a string.
csharp1using System; 2using System.Text.RegularExpressions; 3 4class Program 5{ 6 static void Main() 7 { 8 string input = "The quick brown fox jumps over the lazy dog."; 9 string pattern = @"\blazy\b"; // Match the word "lazy" exactly 10 string replacement = "energetic"; 11 12 string result = Regex.Replace(input, pattern, replacement); 13 14 Console.WriteLine("Original: " + input); 15 Console.WriteLine("Modified: " + result); 16 } 17}