Regex Class
Overview
Good practice
Always include the ^ and $ when matching expressions.
Start your regex with the @ sign for readability, instead of having to escape backslashes.
Examples
string regex = @"^\d{5}$";
Console.WriteLine(Regex.IsMatch("12345", regex) ? "Matches!" : "No match");
Console.WriteLine(Regex.IsMatch("1234", regex) ? "Matches!" : "No match");
Console.WriteLine(Regex.IsMatch("DROP DATABASE YourApp; -- 12345", @"\d{5}$") ? "Matches!" : "No match");
string input = "The dog ate a bone.";
string regex = @"^The (?<animal>\w+)\b ate (?<food>.+)\.$";
Console.WriteLine(Regex.Replace(input, regex, "Animal: ${animal}, Food: ${food}"));
Using Groups
Match x = Regex.Match("Wim", "(?<FirstLetter>.).*");
Console.WriteLine(x.Groups["FirstLetter"]);