string input = "The quick brown fox jumps over the lazy dog."; string pattern = @"\b\w{3}\b"; // Matches three letter words MatchCollection matches = Regex.Matches(input, pattern); foreach (Match match in matches) { Console.WriteLine(match.Value); }
string input = "[email protected]"; string pattern = @"(\w+)@([a-z]+)\.([a-z]{2,3})"; // Matches email address pattern Match match = Regex.Match(input, pattern); if (match.Success) { Console.WriteLine("Username: " + match.Groups[1].Value); // Output: Username: john Console.WriteLine("Domain: " + match.Groups[2].Value); // Output: Domain: example Console.WriteLine("TLD: " + match.Groups[3].Value); // Output: TLD: com }In the above example, we have used the Match class to match an email address pattern and extract the username, domain, and top-level domain (TLD) from the input string. The System.Text.RegularExpressions Match class is a part of the .NET Framework and is located in the System.Text.RegularExpressions namespace.