代码改变世界

正则表达式提取4

2012-08-15 18:07  C#与.NET探索者  阅读(184)  评论(0编辑  收藏  举报
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace 正则表达式案例
{
    class Program
    {
        static void Main(string[] args)
        {
            // string reg = "^b.*g$";
            //bool b = Regex.IsMatch("baag", "^b.*g$");
            //Console.WriteLine(b);

            //bool b = Regex.IsMatch("bg", "^b.*g$");
            //Console.WriteLine(b);

            //bool b = Regex.IsMatch("b\ng", "^b.*g$");
            //Console.WriteLine(b);
            //加了^$,表示用户输入的字符串必须与该正则表达式完全匹配。
            //bool b = Regex.IsMatch("大家bg好哦", "^b.*g$");
            //Console.WriteLine(b);

            ////由于没有加^$,表示在整个字符串中只要找到任意一个与b.*g匹配的字符串就返回true.
            //bool b = Regex.IsMatch("大家好bg哦", "b.*g");
            //Console.WriteLine(b);

            //bool b = Regex.IsMatch("zxh loves this food.lol", "^z|food$");
            //Console.WriteLine(b);
            //Console.ReadKey();

            #region 验证邮编

            //while (true)
            //{
            //    Console.WriteLine("请输入邮政编码:");
            //    string postcode = Console.ReadLine();
            //    //                bool b = Regex.IsMatch(postcode, "^[0-9]{6}$");
            //    bool b = Regex.IsMatch(postcode, @"^\d{6}$");
            //    Console.WriteLine(b);
            //}

            #endregion

            #region 验证身份证号

            ////要求15或18为,且都是数字
            //while (true)
            //{
            //    Console.WriteLine("请输入身份证号:");
            //    string id = Console.ReadLine();
            //    //bool b = Regex.IsMatch(id, @"^(\d{15}|\d{18})$");
            //    bool b = Regex.IsMatch(id, @"^\d{15}$|^\d{18}$");
            //    Console.WriteLine(b);

            //}


            #endregion

            #region 验证电话号码

            //010-8888888或010-88888880或010xxxxxxx
            //0335-8888888或0335-88888888(区号-电话号)
            //10086、10010、95595、95599、95588(5位)
            //13888888888(11位都是数字)
            while (true)
            {
                Console.WriteLine("请输入一个电话号码");
                string phoneNumber = Console.ReadLine();
                // bool b = Regex.IsMatch(phoneNumber, @"^((\d{3,4}\-?\d{7,8})|(\d{5})|(\d{11}))$");
                bool b = Regex.IsMatch(phoneNumber, @"^((\d{3,4}\-?\d{7,8})|(\d{5}))$");
                Console.WriteLine(b);
            }

            #endregion


        }
    }
}