string input = "The price of the product is $199.99"; string pattern = @"\d+"; Match match = Regex.Match(input, pattern); if (match.Success) { Console.WriteLine("Matched digits: " + match.Value); }
string input = "The quick brown fox jumps over the lazy dog."; string pattern = "the"; string replacement = "a"; string result = Regex.Replace(input, pattern, replacement, RegexOptions.IgnoreCase); Console.WriteLine(result);
string email = "[email protected]"; string pattern = @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"; if (Regex.IsMatch(email, pattern)) { Console.WriteLine("Valid email address."); } else { Console.WriteLine("Invalid email address."); }This code checks if the given email address is valid according to the specified pattern. The pattern matches a sequence of one or more word characters, dots, and hyphens followed by an @ symbol, a domain name containing one or more segments separated by dots, and a top-level domain of 2-4 letters. In all of these examples, the System.Text.RegularExpressions namespace is used, which is part of the System.dll assembly in the .NET Framework Class Library.