using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Text.RegularExpressions; namespace _17._4Csharp中正則表達式的匹配 { class Program { static void Main(string[] args) { string pattern = @"^(0349|0349-)\d{7,8}$"; string[] input = { "010-12345678","0349-1234567","03491234567","075912345678","13886133650"}; foreach( string outstr in input) { //靜態方法 // bool mybool = Regex.IsMatch(outstr.Trim(),pattern); // if (mybool) Console.WriteLine(outstr + "是宿州市固話"); // else Console.WriteLine(outstr+"不是宿州固話"); //實例化方法 Match mymatch = Regex.Match(outstr.Trim(),pattern); if (mymatch.Success) Console.WriteLine(outstr + "是宿州市固話"); else Console.WriteLine(outstr+"不是宿州市固話"); } Console.ReadKey(); } } }