chapter18正則表達式:學習“或”元字符

|        將兩個匹配條件進行邏輯“或”運算

()       用小括號指定子表達式(也就是分組)

例如:重複多個(abcd){n}進行分組限定。

string str = "同學們同學們”;

string strPattern = @"(同學們){2}";

Regex.IsMatch(str, strPattern);

源代碼:

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


namespace chapter18學習或運算與小括號元字符
{
    class Program
    {
        /// <summary>
        /// 在文本中,檢索字符或數字
        /// </summary>
        public void Test1()
        {
            string str = "2467889jckclslsll*&^%ghj876";
            string strPattern = @"\d|[a-z]";
            MatchCollection col = Regex.Matches(str, strPattern);
            foreach (Match item in col)
            {
                Console.WriteLine(item);
            }
        }
        /// <summary>
        /// 提取博客的標籤
        /// </summary>
        public void Test2()
        {
            string[] strTagArray;  //標籤數組
            string strTag = "C#,C#教程.C#講義;C#書籍";
            string strPattern = @"[,]|[.]|[;]";
            strTagArray = Regex.Split(strTag, strPattern);
            //顯示結果
            foreach (var item in strTagArray)
            {
                Console.WriteLine(item);
            }
        }
        static void Main(string[] args)
        {
            Program obj = new Program();
            //obj.Test1();
            obj.Test2();
            Console.ReadKey();
        }
    }
}

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