chapter17(2)正則表達式 重複描述字符

{n}   匹配前面的字符n次

{n,}   匹配前面的字符n次或多於n次

{n,m}匹配前面的字符n到m次

?     重複零次或者一次

+      重複一次或者更多次

*       表示零到多個元字符,或表示重複零次或者更多次(即:重複前面的次數)

例如:判斷合法的QQ號(必須5-12位)

string strQQ = “517722017”;

str strPattern = @"\d{5,12}$";

Regex.IsMatch(strQQ, strPattern);


源代碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;


namespace chapter17重複描述字符
{
    class Program
    {
        /// <summary>
        /// QQ合法性判斷(要求全部是數字,且5-12位之間)
        /// </summary>
        public void Test1()
        {
            string qq1 = "1111";
            string qq2 = "876654445";
            string qq3 = "dddf87557";


            //模式字符串
            string strPattern = @"^\d{5,12}$";
            Console.WriteLine(Regex.IsMatch(qq1, strPattern));
            Console.WriteLine(Regex.IsMatch(qq2, strPattern));
            Console.WriteLine(Regex.IsMatch(qq3, strPattern));


        }


        
        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.Test1();
            Console.ReadKey();
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章