如何:使用正则表达式搜索字符串
示例
class TestRegularExpressions { static void Main() { string[] sentences = { "C# code", "Chapter 2: Writing Code", "Unicode", "no match here" }; string sPattern = "code"; foreach (string s in sentences) { System.Console.Write("{0,24}", s); if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase)) { System.Console.WriteLine(" (match for '{0}' found)", sPattern); } else { System.Console.WriteLine(); } } // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } } /* Output: C# code (match for 'code' found) Chapter 2: Writing Code (match for 'code' found) Unicode (match for 'code' found) no match here */
示例
class TestRegularExpressionValidation { static void Main() { string[] numbers = { "123-555-0190", "444-234-22450", "690-555-0178", "146-893-232", "146-555-0122", "4007-555-0111", "407-555-0111", "407-2-5555", }; string sPattern = "^\\d{3}-\\d{3}-\\d{4}$"; foreach (string s in numbers) { System.Console.Write("{0,14}", s); if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern)) { System.Console.WriteLine(" - valid"); } else { System.Console.WriteLine(" - invalid"); } } // Keep the console window open in debug mode. System.Console.WriteLine("Press any key to exit."); System.Console.ReadKey(); } } /* Output: 123-555-0190 - valid 444-234-22450 - invalid 690-555-0178 - valid 146-893-232 - invalid 146-555-0122 - valid 4007-555-0111 - invalid 407-555-0111 - valid 407-2-5555 - invalid */
