幾個經典的正則表達式Regex匹配


 #region 身份證號碼正則表達式
            //while (true)
            //{
            //    Console.WriteLine("請輸入一個身份證號碼");
            //    string id = Console.ReadLine();
            //    bool b4 = Regex.IsMatch(id, @"^\d{15}|\d{18}$");
            //    bool b5 = Regex.IsMatch(id, @"^(\d{15}|\d{18})$");
            //    Console.WriteLine(b4);
            //    Console.WriteLine(b5);
            //}
            #endregion

            #region 匹配電話號碼
            //while (true)
            //{

            //    Console.WriteLine("請輸入電話號碼");
            //    string phone = Console.ReadLine();
            //    bool b = Regex.IsMatch(phone, @"^((\d{3,4}\-\d?{7,8})|(\d{5}))$");
            //    Console.WriteLine(b);
            //}
            #endregion

            #region 匹配email的regex

            //while (true)
            //{
            //    Console.WriteLine("請輸入Email地址");
            //    string email = Console.ReadLine();
            //    bool b = Regex.IsMatch(email, @"^\w+@\w+\.\w+$");
            //    Console.WriteLine(b);
            //}
            #endregion

            #region 匹配ip地址的regex
            //while (true)
            //{
            //    Console.WriteLine("請輸入一個IP地址");
            //    string ip = Console.ReadLine();
            //    bool b = Regex.IsMatch(ip, @"^\d{1,3}(\.\d{1,3}){3}$");
            //    Console.WriteLine(b);
            //}
            #endregion

            #region 匹配日期合法regex
            //while (true)
            //{
            //    Console.WriteLine("請輸入一個日期");
            //    string date = Console.ReadLine();
            //    bool b = Regex.IsMatch(date, @"^\d{4}\-\d{1,2}\-\d{1,2}$");
            //    Console.WriteLine(b);
            //}
            #endregion


            #region 匹配url地址的regex
            //"http://www.itcast.cn"
            //"http://www.test.com/a.htm?id=3&name=aaa"
            //"http://s.taobao.com/search?q=nokia800&keyword=&commend=all&ssid=s5-e&search_type=item&atype=&tracelog=&sourceId=tb.index&initiative_id=tbindexz_20120223"
            //"ftp://127.0.0.1/1.txt"

            while (true)
            {
                Console.WriteLine("請輸入url地址");
                string url = Console.ReadLine();
                bool b = Regex.IsMatch(url, @"^[a-zA-Z]+://.+$");
                Console.WriteLine(b);
            }
            #endregion


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM