asp.net中Regex使用(二)

來源:互聯網
上載者:User

Regex

Regex 類表示不可變(唯讀)Regex類。它還包含各種靜態方法,允許在不顯式建立其他類的執行個體的情況下使用其他Regex類。

以下程式碼範例建立了 Regex 類的執行個體並在初始化對象時定義一個簡單的Regex。請注意,使用了附加的反斜線作為逸出字元,它將 \s 匹配字元類中的反斜線指定為原義字元。

[Visual Basic]    ' Declare object variable of type Regex.    Dim r As Regex     ' Create a Regex object and define its regular expression.    r = New Regex("\s2000")

 

[C#]    // Declare object variable of type Regex.    Regex r;     // Create a Regex object and define its regular expression.    r = new Regex("\\s2000");

 

Match

Match 類表示Regex匹配操作的結果。以下樣本使用 Regex 類的 Match 方法返回 Match 類型的對象,以便找到輸入字串中的第一個匹配項。此樣本使用 Match 類的 Match.Success 屬性來指示是否已找到匹配。

[Visual Basic]    ' cCreate a new Regex object.    Dim r As New Regex("abc")     ' Find a single match in the input string.    Dim m As Match = r.Match("123abc456")     If m.Success Then        ' Print out the character position where a match was found.         ' (Character position 3 in this case.)        Console.WriteLine("Found match at position " & m.Index.ToString())    End If

 

[C#]    // Create a new Regex object.    Regex r = new Regex("abc");     // Find a single match in the string.    Match m = r.Match("123abc456");     if (m.Success)     {        // Print out the character position where a match was found.         // (Character position 3 in this case.)        Console.WriteLine("Found match at position " + m.Index);    }

 

MatchCollection

MatchCollection 類表示成功的非重疊匹配的序列。該集合為不可變(唯讀)的,並且沒有公用建構函式。MatchCollection 的執行個體是由 Regex.Matches 屬性返回的。

以下樣本使用 Regex 類的 Matches 方法,通過在輸入字串中找到的所有匹配填充 MatchCollection。該樣本將此集合複製到一個字串數組和一個整數數組中,其中字串數組用以儲存每個匹配項,整數數組用以指示每個匹配項的位置。

[Visual Basic]    Dim mc As MatchCollection    Dim results(20) As String    Dim matchposition(20) As Integer    ' Create a new Regex object and define the regular expression.    Dim r As New Regex("abc")    ' Use the Matches method to find all matches in the input string.    mc = r.Matches("123abc4abcd")    ' Loop through the match collection to retrieve all     ' matches and positions.    Dim i As Integer    For i = 0 To mc.Count - 1        ' Add the match string to the string array.        results(i) = mc(i).Value        ' Record the character position where the match was found.        matchposition(i) = mc(i).Index    Next i

 

[C#]    MatchCollection mc;    String[] results = new String[20];    int[] matchposition = new int[20];        // Create a new Regex object and define the regular expression.    Regex r = new Regex("abc");     // Use the Matches method to find all matches in the input string.    mc = r.Matches("123abc4abcd");    // Loop through the match collection to retrieve all     // matches and positions.    for (int i = 0; i < mc.Count; i++)     {        // Add the match string to the string array.           results[i] = mc[i].Value;        // Record the character position where the match was found.        matchposition[i] = mc[i].Index;       }

 

GroupCollection

GroupCollection 類表示捕獲的組的集合并返回單個匹配中捕獲的組的集合。該集合為不可變(唯讀)的,並且沒有公用建構函式。GroupCollection 的執行個體在 Match.Groups 屬性返回的集合中返回。

以下控制台應用程式樣本尋找並輸出由Regex捕獲的組的數目。有關如何提取組集合的每一成員中的各個捕獲項的樣本,請參見下面一節的 Capture Collection 樣本。

[Visual Basic]    Imports System    Imports System.Text.RegularExpressions    Public Class RegexTest        Public Shared Sub RunTest()            ' Define groups "abc", "ab", and "b".            Dim r As New Regex("(a(b))c")             Dim m As Match = r.Match("abdabc")            Console.WriteLine("Number of groups found = " _            & m.Groups.Count.ToString())        End Sub                Public Shared Sub Main()            RunTest()        End Sub    End Class

 

[C#]    using System;    using System.Text.RegularExpressions;    public class RegexTest     {        public static void RunTest()         {            // Define groups "abc", "ab", and "b".            Regex r = new Regex("(a(b))c");             Match m = r.Match("abdabc");            Console.WriteLine("Number of groups found = " + m.Groups.Count);        }        public static void Main()         {            RunTest();        }    }

 

該樣本產生下面的輸出。

[Visual Basic]    Number of groups found = 3

 

[C#]    Number of groups found = 3

 

CaptureCollection

CaptureCollection 類表示捕獲的子字串的序列,並且返回由單個擷取的群組執行的捕獲的集合。由於限定符,擷取的群組可以在單個匹配中捕獲多個字串。Captures 屬性(CaptureCollection 類的對象)是作為 Match 和 group 類的成員提供的,以便於對捕獲的子字串的集合的訪問。

例如,如果使用Regex ((a(b))c)+(其中 + 限定符指定一個或多個匹配)從字串“abcabcabc”中捕獲匹配,則子字串的每一匹配的 GroupCaptureCollection 將包含三個成員。

以下控制台應用程式樣本使用Regex (Abc)+ 來尋找字串“XYZAbcAbcAbcXYZAbcAb”中的一個或多個匹配。該樣本闡釋了使用 Captures 屬性來返回多組捕獲的子字串。

[Visual Basic]    Imports System    Imports System.Text.RegularExpressions    Public Class RegexTest        Public Shared Sub RunTest()            Dim counter As Integer            Dim m As Match            Dim cc As CaptureCollection            Dim gc As GroupCollection            ' Look for groupings of "Abc".            Dim r As New Regex("(Abc)+")             ' Define the string to search.            m = r.Match("XYZAbcAbcAbcXYZAbcAb")            gc = m.Groups                        ' Print the number of groups.            Console.WriteLine("Captured groups = " & gc.Count.ToString())                        ' Loop through each group.            Dim i, ii As Integer            For i = 0 To gc.Count - 1                cc = gc(i).Captures                counter = cc.Count                                ' Print number of captures in this group.                Console.WriteLine("Captures count = " & counter.ToString())                                ' Loop through each capture in group.                            For ii = 0 To counter - 1                    ' Print capture and position.                    Console.WriteLine(cc(ii).ToString() _                        & "   Starts at character " & cc(ii).Index.ToString())                Next ii            Next i        End Sub            Public Shared Sub Main()            RunTest()         End Sub    End Class

 

[C#]    using System;    using System.Text.RegularExpressions;    public class RegexTest         {        public static void RunTest()         {            int counter;            Match m;            CaptureCollection cc;            GroupCollection gc;            // Look for groupings of "Abc".            Regex r = new Regex("(Abc)+");             // Define the string to search.            m = r.Match("XYZAbcAbcAbcXYZAbcAb");             gc = m.Groups;            // Print the number of groups.            Console.WriteLine("Captured groups = " + gc.Count.ToString());            // Loop through each group.            for (int i=0; i < gc.Count; i++)             {                cc = gc[i].Captures;                counter = cc.Count;                                // Print number of captures in this group.                Console.WriteLine("Captures count = " + counter.ToString());                                // Loop through each capture in group.                for (int ii = 0; ii < counter; ii++)                 {                    // Print capture and position.                    Console.WriteLine(cc[ii] + "   Starts at character " +                         cc[ii].Index);                }            }        }        public static void Main() {            RunTest();        }    }

 

此樣本返回下面的輸出結果。

[Visual Basic]    Captured groups = 2    Captures count = 1    AbcAbcAbc   Starts at character 3    Captures count = 3    Abc   Starts at character 3    Abc   Starts at character 6    Abc   Starts at character 9

 

[C#]    Captured groups = 2    Captures count = 1    AbcAbcAbc   Starts at character 3    Captures count = 3    Abc   Starts at character 3    Abc   Starts at character 6    Abc   Starts at character 9

 

Group

group 類表示來自單個擷取的群組的結果。因為 Group 可以在單個匹配中捕獲零個、一個或更多的字串(使用限定符),所以它包含 Capture 對象的集合。因為 Group 繼承自 Capture,所以可以直接存取最後捕獲的子字串(Group 執行個體本身等價於 Captures 屬性返回的集合的最後一項)。

Group 的執行個體是由 Match.Groups(groupnum) 屬性返回的,或者在使用“(?<groupname>)”分組構造的情況下,是由 Match.Groups("groupname") 屬性返回的。

以下程式碼範例使用嵌套的分組構造來將子字串捕獲到組中。

[Visual Basic]    Dim matchposition(20) As Integer    Dim results(20) As String    ' Define substrings abc, ab, b.    Dim r As New Regex("(a(b))c")     Dim m As Match = r.Match("abdabc")    Dim i As Integer = 0    While Not (m.Groups(i).Value = "")           ' Copy groups to string array.       results(i) = m.Groups(i).Value            ' Record character position.        matchposition(i) = m.Groups(i).Index         i = i + 1    End While

 

[C#]    int[] matchposition = new int[20];    String[] results = new String[20];    // Define substrings abc, ab, b.    Regex r = new Regex("(a(b))c");     Match m = r.Match("abdabc");    for (int i = 0; m.Groups[i].Value != ""; i++)     {        // Copy groups to string array.        results[i]=m.Groups[i].Value;         // Record character position.        matchposition[i] = m.Groups[i].Index;     }

 

此樣本返回下面的輸出結果。

[Visual Basic]    results(0) = "abc"   matchposition(0) = 3    results(1) = "ab"    matchposition(1) = 3    results(2) = "b"     matchposition(2) = 4

 

[C#]    results[0] = "abc"   matchposition[0] = 3    results[1] = "ab"    matchposition[1] = 3    results[2] = "b"     matchposition[2] = 4

 

以下程式碼範例使用命名的分組構造,從包含“DATANAME:VALUE”格式的資料的字串中捕獲子字串,Regex通過冒號“:”拆分資料。

[Visual Basic]    Dim r As New Regex("^(?<name>\w+):(?<value>\w+)")    Dim m As Match = r.Match("Section1:119900")

 

[C#]    Regex r = new Regex("^(?<name>\\w+):(?<value>\\w+)");    Match m = r.Match("Section1:119900");

 

此Regex返回下面的輸出結果。

[Visual Basic]    m.Groups("name").Value = "Section1"    m.Groups("value").Value = "119900"

 

[C#]    m.Groups["name"].Value = "Section1"    m.Groups["value"].Value = "119900"

 

Capture

Capture 類包含來自單個子運算式捕獲的結果。

以下樣本在 Group 集合中迴圈,從 Group 的每一成員中提取 Capture 集合,並且將變數 posn length 分別分配給找到每一字串的初始字串中的字元位置,以及每一字串的長度。

[Visual Basic]    Dim r As Regex    Dim m As Match    Dim cc As CaptureCollection    Dim posn, length As Integer    r = New Regex("(abc)*")    m = r.Match("bcabcabc")    Dim i, j As Integer    i = 0    While m.Groups(i).Value <> ""        ' Grab the Collection for Group(i).        cc = m.Groups(i).Captures        For j = 0 To cc.Count - 1            ' Position of Capture object.            posn = cc(j).Index            ' Length of Capture object.            length = cc(j).Length        Next j        i += 1    End While

 

[C#]    Regex r;    Match m;    CaptureCollection cc;    int posn, length;    r = new Regex("(abc)*");    m = r.Match("bcabcabc");    for (int i=0; m.Groups[i].Value != ""; i++)     {        // Capture the Collection for Group(i).        cc = m.Groups[i].Captures;         for (int j = 0; j < cc.Count; j++)         {            // Position of Capture object.            posn = cc[j].Index;             // Length of Capture object.            length = cc[j].Length;         }    }

 

 

private void button1_Click(object sender, System.EventArgs e)
   {
    Regex regexTest = new Regex(@"[0-9]H",RegexOptions.IgnoreCase);
    Match match = regexTest.Match(this.textBox1.Text);
    System.Text.StringBuilder strb = new System.Text.StringBuilder();

    GroupCollection groups = match.Groups;    #region "while"   
    while (match.Success)
    {  
     if (strb.Length == 0)
     {
      strb.Append("Match Succeeded!");
     }
     for (int index = 0;index < groups.Count;index++)
     {
      strb.Append(Environment.NewLine);
      strb.Append(groups[index].Value);
     }
     match = match.NextMatch();
    }
    #endregion    if (strb.Length == 0)
    {
     strb.Append("Match Failed!");
    }
    this.textBox2.Text = strb.ToString();   }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.