Regular Expressions (System.Text.RegularExpressions)
Check if pattern matches input
Section titled “Check if pattern matches input”public bool Check(){ string input = "Hello World!"; string pattern = @"H.ll. W.rld!";
// true return Regex.IsMatch(input, pattern);}Passing Options
Section titled “Passing Options”public bool Check(){ string input = "Hello World!"; string pattern = @"H.ll. W.rld!";
// true return Regex.IsMatch(input, pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline);}Match into groups
Section titled “Match into groups”public string Check(){ string input = "Hello World!"; string pattern = @"H.ll. (?<Subject>W.rld)!";
Match match = Regex.Match(input, pattern);
// World return match.Groups["Subject"].Value;}Remove non alphanumeric characters from string
Section titled “Remove non alphanumeric characters from string”public string Remove(){ string input = "Hello./!";
return Regex.Replace(input, "[^a-zA-Z0-9]", "");}Simple match and replace
Section titled “Simple match and replace”public string Check(){ string input = "Hello World!"; string pattern = @"W.rld";
// Hello Stack Overflow! return Regex.Replace(input, pattern, "Stack Overflow");}Find all matches
Section titled “Find all matches”using System.Text.RegularExpressions;static void Main(string[] args){ string input = "Carrot Banana Apple Cherry Clementine Grape"; // Find words that start with uppercase 'C' string pattern = @"\bC\w*\b";
MatchCollection matches = Regex.Matches(input, pattern); foreach (Match m in matches) Console.WriteLine(m.Value);}Output
Section titled “Output”CarrotCherryClementine