程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C#之正則表達式使用實例講解

C#之正則表達式使用實例講解

編輯:C#入門知識

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

namespace 字符串
{
    class 正則表達式
    {
        public static void Main()
        {
            //正則表達式是用來簡化字符串操作的,一般正則所實現的功能,用方法也一樣可以,只是方法有可能就會變的很復雜!
            //假定我們要在字符串中做一次純文件的搜索,不用任何的正則串。
            string Text = @"This comprehensive compendium provides a boroad and thor
                            This comprehensive compendium provides a boroad and thor
                            This comprehensive compendium provides a boroad and thor
                            This comprehensive compendium provides a boroad and thor
                            This comprehensive compendium provides a boroad and thor";
            string Pattern = "thor";

            MatchCollection Matchs = Regex.Matches( Text , Pattern , RegexOptions.IgnoreCase );

            foreach (Match i in Matchs)
            {
                Console.WriteLine( i.Value );
            }
            
            //例:我們要查看我們的單詞是不是以N開頭
            string myText = "Naladdin";
            Match mat = Regex.Match( myText , @"n" , RegexOptions.IgnoreCase );
            Console.WriteLine(myText "是否以N開頭" mat.Success);

            //以A開頭,以ion結尾
            string myText2 = "Applaldafaslkdfasd;fasdfasdfasdfadsfadfadsfication";
            Match mat2 = Regex.Match(myText2, @"aS*ion", RegexOptions.IgnoreCase);
            Console.WriteLine(myText2 "是否以A開頭Ion結尾,任意長度的單詞" mat2.Success);

            Console.ReadLine();
            
        }
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved