string input = "The quick brown fox jumps over the lazy dog"; string pattern = "fox"; MatchCollection matches = Regex.Matches(input, pattern); foreach (Match match in matches) { Console.WriteLine(match.Value); }
string input = "Please send an email to [email protected] or [email protected]"; string pattern = @"\w+@\w+\.\w+"; MatchCollection matches = Regex.Matches(input, pattern); foreach (Match match in matches) { Console.WriteLine(match.Value); }In this example, we are using a regular expression pattern to match all email addresses in the input string. The pattern @"\w+@\w+\.\w+" specifies that the email address must contain one or more word characters before the "@" symbol, followed by one or more word characters before the ".", followed by one or more word characters after the ".". The Regex.Matches method returns a collection of Match objects that represent the matches found in the input string. We then loop through each match and output its value, which in this case will be "[email protected]" and "[email protected]". Overall, the System.Text.RegularExpressions.Regex Matches class provides a powerful and flexible way to search and match patterns in strings using regular expressions.