string input = "The quick brown fox jumps over the lazy dog"; string pattern = @"\b\w{5}\b"; MatchCollection matches = Regex.Matches(input, pattern); foreach (Match match in matches) { Console.WriteLine(match.ToString()); }
string input = "I have 10 cats and 5 dogs"; string pattern = @"\d+"; Match match = Regex.Match(input, pattern); Console.WriteLine(match.ToString());This code matches the first occurrence of one or more digits in the input string, and using the ToString method, prints the match to the console. The System.Text.RegularExpressions package library provides the classes and methods needed for working with regular expressions in C#.