規則運算式類別

下列章節說明 .NET Framework 規則運算式類別。

Regex

Regex 類別表示不變的 (唯讀) 規則運算式。它也包含靜態方法,允許使用其他規則運算式類別,而不需明確建立其他類別的執行個體。

下列程式碼範例建立 Regex 類別的執行個體,並在物件初始化時定義簡單規則運算式。請注意將額外反斜線當做逸出字元的用法,表示在 \s 比對字元類別中的反斜線為常值字元。

    ' Declare object variable of type Regex.
    Dim r As Regex 
    ' Create a Regex object and define its regular expression.
    r = New Regex("\s2000")
    // Declare object variable of type Regex.
    Regex r; 
    // Create a Regex object and define its regular expression.
    r = new Regex("\\s2000"); 

Match

Match 類別表示規則運算式比對作業的結果。下列範例使用 Regex 類別的 Match 方法,傳回 Match 型別的物件,以便尋找輸入字串中的第一個相符比對。範例使用 Match 類別的 Match.Success 屬性,指示是否找到相符比對。

    ' 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
    // 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。下列範例複製集合至字串陣列 (存放各個相符比對) 和整數陣列 (指示各個相符比對的位置)。

    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
    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 屬性所傳回的集合傳回。

下列主控台應用程式 (Console Application) 範例尋找並印出規則運算式所擷取的群組數目。如需如何在群組集合各個成員中擷取個別擷取的範例,請參閱下個章節中的 Capture Collection 範例。

    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
    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();
        }
    }

這個範例產生下列輸出。

    Number of groups found = 3
    Number of groups found = 3

CaptureCollection

CaptureCollection 類別代表擷取的子字串序列,並傳回單一擷取群組所產生的擷取集合。因為數量詞的緣故,擷取群組可以在單一比對中擷取一個以上的字串。Captures 屬性 (CaptureCollection 類別的物件) 被提供做為 MatchGroup 類別的成員,以加速存取擷取的子字串集合。

例如,如果您使用規則運算式 ((a(b))c)+ (其中 + 數量詞會指定一或多個比對) 從字串 "abcabcabc" 擷取符合項目,對於子字串每一個比對 GroupCaptureCollection 將包含三個成員。

下列主控台應用程式範例使用規則運算式 (Abc)+,在字串 "XYZAbcAbcAbcXYZAbcAb" 中尋找一或多個符合項目。範例說明 Captures 屬性的使用,傳回擷取子字串的多個群組。

    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
    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();
        }
    }

這個範例傳回下列輸出。

    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
    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 的執行個體會藉由編寫 Groups 屬性傳回之 GroupCollection 物件的索引來傳回。如果使用了 "(?<groupname>)" 群組建構,則索引子 (Indexer) 可以是群組編號或擷取群組的名稱。例如,在 C# 程式碼中,您可以使用 Match.Groups[groupnum]Match.Groups["groupname"],或者在 Visual Basic 程式碼中,您可以使用 Match.Groups(groupnum)Match.Groups("groupname")

下列程式碼範例使用巢狀群組建構,將子字串擷取到群組內。

    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
    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; 
    }

這個範例傳回下列輸出。

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

下列程式碼範例使用命名的群組建構,從含有 "DATANAME:VALUE" 格式 (規則運算式以冒號 (":") 分隔) 資料的字串中擷取子字串。

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

這個規則運算式傳回下列輸出。

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

Capture

Capture 類別包含單一子運算式 (Subexpression) 所擷取的結果。

下列範例會對 Group 集合執行迴圈 (Loop),從 Group 的每一個成員中擷取 Capture 集合,並分別將變數 posnlength 指派為原始字串中每一個找到字串的字元位置和每一個字串的長度。

    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
    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; 
        }
    }

請參閱

參考

System.Text.RegularExpressions

其他資源

.NET Framework 規則運算式