C#正則表達式

 Capture: 包含一次匹配的結果; 

                     CaptureCollection: Capture的序列; 
                     Group: 一次組記錄的結果,由Capture繼承而來; 
                     GroupCollection:表示捕獲組的集合
                     Match: 一次表達式的匹配結果,由Group繼承而來; 
                     MatchCollection: Match的一個序列; 
                     MatchEvaluator: 執行替換操作時使用的委託; 
                     Regex:編譯後的表達式的實例。 
                     RegexCompilationInfo:提供編譯器用於將正則表達式編譯爲獨立程序集的信息
                     RegexOptions 提供用於設置正則表達式的枚舉值
Regex類中還包含一些靜態的方法: 
                    Escape: 對字符串中的regex中的轉義符進行轉義; 
                    IsMatch: 如果表達式在字符串中匹配,該方法返回一個布爾值; 
                    Match: 返回Match的實例; 
                    Matches: 返回一系列的Match的方法; 
                    Replace: 用替換字符串替換匹配的表達式; 
                    Split: 返回一系列由表達式決定的字符串; 
                    Unescape:不對字符串中的轉義字符轉義。
首先從使用Regex、Match類的簡單表達式開始學習:
 
 
 
Code
            Regex emailregex = new Regex("(?[^@]+)@(?.+)");
            String s = "[email protected]";
            Match m = emailregex.Match(s);
            if ( m.Success ) 
            {
                System.Console.WriteLine("User: " + m.Groups["user"].Value);
                System.Console.WriteLine("Host: " + m.Groups["host"].Value);
            } 
            else 
            {
                System.Console.WriteLine(s + " is not a valid email address");
            }
IsMatch方法指示 Regex 構造函數中指定的正則表達式在輸入字符串中是否找到匹配項。這是我們使用C#正則表達式時最常用的方法之一。下面的例子說明了IsMatch方法的使用:
 
 
Code
            Regex emailregex = new Regex("(?[^@]+)@(?.+)");
            if(emailregex.IsMatch("ddd*.com"))
            {
                System.Console.WriteLine("Matched successfully");
            }
            else
            {
                System.Console.WriteLine("Matched failed");
            }
            System.Console.ReadLine();
Split方法是把由正則表達式匹配項定義的位置將輸入字符串拆分爲一個子字符串數組。例如:
 
 
 
Code
Regex r = new Regex("-"); // Split on hyphens.
string[] s = r.Split("first-second-third");
for(int i=0;is.Length;i++)
{
Response.Write(s+"
");
}
 
執行的結果是:
First
Second
Third
看上去和String的Split方法一樣,但string的Split方法在由正則表達式而不是一組字符確定的分隔符處拆分字符串。
Match方法是在輸入字符串中搜索正則表達式的匹配項,並Regex 類的 Match 方法返回 Match 對象,Match 類表示正則表達式匹配操作的結果。下面的例子演示Match方法的使用,並利用Match對象的Group屬性返回Group對象:
 
 
Code
string text = @"public string testMatchObj string s string  match ";
string pat = @"(\w+)\s+(string)";
// Compile the regular expression.
Regex r = new Regex(pat, RegexOptions.IgnoreCase);
// Match the regular expression pattern against a text string.
Match m = r.Match(text);
int matchCount = 0;
while (m.Success) 
{
Response.Write("Match"+ (++matchCount) + "
");
for (int i = 1; i  2; i++) 
{
  Group g = m.Groups;
  Response.Write("Group"+i+"='" + g + "'"  + "
");
  CaptureCollection cc = g.Captures;
  for (int j = 0; j  cc.Count; j++) 
  {
   Capture c = cc[j];
   Response.Write("Capture"+j+"='" + c + "', Position="+c.Index + "
");
  }
}
m = m.NextMatch();
}
該事例運行結果是:
Match1
Group1='public'
Capture0='public', Position=0
Group2='string'
Capture0='string', Position=7
Match2
Group1='testMatchObj'
Capture0='testMatchObj', Position=14
Group2='string'
Capture0='string', Position=27
Match3
Group1='s'
Capture0='s', Position=34
Group2='string'
Capture0='string', Position=36
分享到:
QQ空間
騰訊微博
騰訊朋友
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章