C#正則表達式Regex類


C#正則表達式Regex類的使用

C#中為正則表達式的使用提供了非常強大的功能,這就是Regex類。這個包包含於System.Text.RegularExpressions命名空間下面,而這個命名空間所在DLL基本上在所有的項目模板中都不需要單獨去添加引用,可以直接使用。

1、定義一個Regex類的實例


Regex regex = new Regex(@"\d");
這里的初始化參數就是一個正則表達式,“\d”表示配置數字。

2、判斷是否匹配

判斷一個字符串,是否匹配一個正則表達式,在Regex對象中,可以使用Regex.IsMatch(string)方法。

regex.IsMatch("abc"); //返回值為false,字符串中未包含數字
regex.IsMatch("abc3abc"); //返回值為true,因為字符串中包含了數字

3、獲取匹配次數

使用Regex.Matches(string)方法得到一個Matches集合,再使用這個集合的Count屬性。

regex.Matches("abc123abc").Count; 
返回值為3,因為匹配了三次數字。

4、獲取匹配的內容

使用Regex.Match(string)方法進行匹配。

regex.Match("abc123abc").Value;
返回值為1,表示第一個匹配到的值。

5、捕獲

正則表達式中可以使用括號對部分值進行捕獲,要想獲取捕獲的值,可以使用Regex.Match(string).Groups[int].Value來獲取。

Regex regex = new Regex(@"\w(\d*)\w"); //匹配兩個字母間的數字串
regex.Match("abc123abc").Groups[0].Value; //返回值為“123”。

 

using System;  
using System.Text.RegularExpressions;   
namespace Magci.Test.Strings  
{      
    public class TestRegular      
    {          
        public static void WriteMatches(string str, MatchCollection matches)          
        {              
            Console.WriteLine("\nString is : " + str);              
            Console.WriteLine("No. of matches : " + matches.Count);              
            foreach (Match nextMatch in matches)              
            {                  
                //取出匹配字符串和最多10個外圍字符                  
                int Index = nextMatch.Index;                  
                string result = nextMatch.ToString();                  
                int charsBefore = (Index < 5) ? Index : 5;                  
                int fromEnd = str.Length - Index - result.Length;                  
                int charsAfter = (fromEnd < 5) ? fromEnd : 5;                  
                int charsToDisplay = charsBefore + result.Length + charsAfter;                   
                Console.WriteLine("Index: {0},\tString: {1},\t{2}", Index, result, str.Substring(Index - charsBefore, charsToDisplay));             
            }          
        }           
        
        public static void Main()          
        {              
            string Str = @"My name is Magci, for short mgc. I like c sharp!";               
            //查找“gc”              
            string Pattern = "gc";              
            MatchCollection Matches = Regex.Matches(Str, Pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);               
            WriteMatches(Str, Matches);                            
            //查找以“m”開頭,“c”結尾的單詞              
            Pattern = @"\bm\S*c\b";              
            Matches = Regex.Matches(Str, Pattern, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture);               
            WriteMatches(Str, Matches);          
        }      
    }  
} 

 

http://www.splaybow.com/csharp-regex-class.shtml

http://developer.51cto.com/art/200908/144164.htm


免責聲明!

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



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