1. 程式人生 > >C#正則表示式用於手機號判斷

C#正則表示式用於手機號判斷

*********************用手機號註冊帳號對手機號判斷(正則表示式判斷)***********************************/ 




while (true)
            {
                Console.WriteLine("輸入手機號,判斷是否合規");
                string phonenumber = Console.ReadLine();
                //正則判斷手機號是否合規
                string path = @"((^13[0-9]{1}[0-9]{8}|^15[0-9]{1}[0-9]{8}|^14[0-9]{1}[0-9]{8}|^16[0-9]{1}[0-9]{8}|^17[0-9]{1}[0-9]{8}|^18[0-9]{1}[0-9]{8}|^19[0-9]{1}[0-9]{8})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)";
              
                if (phonenumber.Length == 11)
                {
                    //public static bool IsMatch(string input, string pattern);bool型別
                    bool isphone = Regex.IsMatch(phonenumber, path);
                    Console.WriteLine("電話:" + isphone);
                    if (isphone)
                    {
                        Console.WriteLine("手機號符合要求");
                    }
                    else
                    {
                        Console.WriteLine("手機號不符合要求");
                    }
                }
                else
                {
                    Console.WriteLine("手機號長度不夠11位");
                }

            }