string input = "My phone number is 123-456-7890"; string pattern = @"(\d{3})-(\d{3})-(\d{4})"; Match match = Regex.Match(input, pattern); if (match.Success) { Console.WriteLine("Phone number found: " + match.Value); }
string input = "I have 5 apples and 3 oranges"; string pattern = @"\d+"; string replacement = "fruit"; string output = Regex.Replace(input, pattern, replacement); Console.WriteLine(output);In this example, we use the Match class to replace all numeric digits in the input string with the word "fruit". We define a pattern using regular expressions to match any sequence of one or more digits, and use the Replace method to substitute this pattern with the replacement string "fruit". Package library: System.Text.RegularExpressions