This part contains step-by-step, real-world C#
examples using Regular Expressions for:
- Email
and phone number validation
- Password
strength checking
- Extracting
URLs and domains
- Parsing
log files
- Cleaning
HTML tags
- Renaming
files in bulk
- Web
scraping-like text processing
- Form
input validation
- File
path parsing
- Text
analytics (word frequency, tokenization)
4.1 Validate Email Addresses
string pattern = @"^[\w\.-]+@[\w\.-]+\.\w{2,}$"; string input = "john.doe@example.com"; bool valid = Regex.IsMatch(input, pattern); Console.WriteLine(valid); // True
🧠Explanation:
- ^[\w\.-]+
→ Start with word characters, dot, or dash
- @ →
Literal at-symbol
- [\w\.-]+
→ Domain name
- \.\w{2,}$
→ Dot + at least 2 letters for TLD
4.2 Validate Phone Numbers (US)
string pattern = @"^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$"; string[] samples = { "123-456-7890", "(123) 456 7890", "123.456.7890", "1234567890" }; foreach (var phone in samples) Console.WriteLine($"{phone}: {Regex.IsMatch(phone, pattern)}");
4.3 Password Strength Checker
string pattern = @"^(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+]).{8,}$"; string input = "Pa$$w0rd123"; bool strong = Regex.IsMatch(input, pattern); Console.WriteLine(strong); // True
✅ Requires uppercase, digit,
special character, and 8+ characters
4.4 Extract URLs from Text
string pattern = @"https?://[^\s]+"; string text = "Visit https://openai.com or http://example.org for details."; foreach (Match m in Regex.Matches(text, pattern)) Console.WriteLine(m.Value);
4.5 Parse Log Entries
Example log:
[2025-08-01 12:34:56] ERROR: Disk failure on drive C
string pattern = @"\[(?<timestamp>.+?)\]\s(?<level>[A-Z]+):\s(?<message>.+)"; string input = "[2025-08-01 12:34:56] ERROR: Disk failure on drive C"; Match m = Regex.Match(input, pattern); if (m.Success) { Console.WriteLine($"Time: {m.Groups["timestamp"]}"); Console.WriteLine($"Level: {m.Groups["level"]}"); Console.WriteLine($"Message: {m.Groups["message"]}"); }
4.6 Strip HTML Tags
string html = "<div><p>Hello <strong>world</strong></p></div>"; string noTags = Regex.Replace(html, "<.*?>", ""); Console.WriteLine(noTags); // Hello world
4.7 Rename Files in Bulk
Imagine renaming file names like 2025_report_final.txt to report_2025.txt
string pattern = @"(\d{4})_(\w+)_final\.txt"; string input = "2025_report_final.txt"; string result = Regex.Replace(input, pattern, "$2_$1.txt"); Console.WriteLine(result); // report_2025.txt
4.8 Validate User Input (Form Example)
Username Rules:
- Only
letters/numbers
- Length:
4–12
string pattern = @"^[a-zA-Z0-9]{4,12}$"; string[] usernames = { "admin", "root123", "bad user", "too_long_username" }; foreach (string name in usernames) Console.WriteLine($"{name}: {Regex.IsMatch(name, pattern)}");
4.9 Extract File Paths and Extensions
string pattern = @"(?<name>[\w\-]+)\.(?<ext>jpg|png|gif)"; string input = "img1.jpg, banner.png, doc.txt"; foreach (Match m in Regex.Matches(input, pattern)) { Console.WriteLine($"File: {m.Groups["name"]}, Ext: {m.Groups["ext"]}"); }
4.10 Word Frequency Counter (Tokenization)
string text = "Regex is powerful. Regex is flexible."; var matches = Regex.Matches(text.ToLower(), @"\w+"); var freq = new Dictionary<string, int>(); foreach (Match m in matches) { string word = m.Value; if (!freq.ContainsKey(word)) freq[word] = 0; freq[word]++; } foreach (var kv in freq) Console.WriteLine($"{kv.Key}: {kv.Value}");
Outputs:
regex: 2
is: 2
powerful: 1
flexible: 1
4.11 Clean & Normalize Input
Remove multiple spaces, trim start/end:
string input = " This is a test. "; string cleaned = Regex.Replace(input.Trim(), @"\s+", " "); Console.WriteLine(cleaned); // This is a test.
4.12 Match Dates in Various Formats
string pattern = @"\b(\d{1,2})[/-](\d{1,2})[/-](\d{2,4})\b"; string input = "Today is 8/4/2025 or 04-08-25"; foreach (Match m in Regex.Matches(input, pattern)) { Console.WriteLine($"Date: {m.Value}"); }
4.13 Advanced: Matching Balanced HTML Tags (Simplified)
string pattern = @"<(?<tag>\w+)>.*?</\k<tag>>"; string html = "<b>Hello</b><i>World</i><p>Ignore</div>"; foreach (Match m in Regex.Matches(html, pattern)) { Console.WriteLine(m.Value); }
⚠️ Not for deeply nested tags —
use parsers like HtmlAgilityPack for HTML trees
4.15 Test-Driven Validation with Regex
Unit test email format with Regex:
Assert.IsTrue(Regex.IsMatch("test@openai.com", @"^[\w\.-]+@[\w\.-]+\.\w{2,}$"));
Integrate regex with unit tests in xUnit, NUnit,
or MSTest for CI/CD.
4.16 Summary
In this practical C#-focused chapter, we’ve used regex to:
✅ Validate and extract structured
data
✅
Parse, clean, and transform strings
✅
Automate file handling
✅
Analyze text content
With regex, you can remove repetitive boilerplate and write
more expressive, compact, and reliable code.
🔜 Coming Next
In Part 5, we’ll cover:
Regex Performance and Optimization in C#, including:
- How
regex engines work under the hood
- Avoiding
catastrophic backtracking
- Compiled
regex
- Benchmarking
pattern efficiency
- Profiling
& tuning patterns in .NET apps
Comments
Post a Comment