Group 類別

定義

代表單一擷取群組的結果。

public ref class Group : System::Text::RegularExpressions::Capture
public class Group : System.Text.RegularExpressions.Capture
[System.Serializable]
public class Group : System.Text.RegularExpressions.Capture
type Group = class
    inherit Capture
[<System.Serializable>]
type Group = class
    inherit Capture
Public Class Group
Inherits Capture
繼承
Group
衍生
屬性

備註

擷取群組可以擷取單一比對中的零、一或多個字串,因為數量詞。 (如需詳細資訊,請參閱 數量詞.) 可從 Group.Captures 屬性取得與單一擷取群組相符的所有子字串。 您可以直接從 ValueIndex 屬性存取最後一個擷取子字串的相關資訊。 (也就是說, Group 實例相當於 屬性所 Captures 傳回之集合的最後一個專案,這會反映擷取群組所建立的最後一個擷取。)

範例有助於厘清 物件與 System.Text.RegularExpressions.CaptureCollection 屬性所傳回之 Captures 之間的 Group 這個關聯性。 正則運算式模式 (\b(\w+?)[,:;]?\s?)+[?.!] 會比對整個句子。 規則運算式的定義如下表所示。

模式 描述
\b 開始字緣比對。
(\w+?) 比對一或多個字元,但字元數愈少愈好。 這是第二個 (內部) 擷取群組。 (第一個擷取群組包含 \b language element.)
[,:;]? 比對零或一次出現的逗號、冒號或分號。
\s? 比對零或一次出現空白字元。
(\b(\w+?)[,:;]?\s?)+ 比對包含字界限、一或多個文字字元、標點符號,以及一或多個空白字元的模式。 這是第一個擷取群組。
[?.!] 比對任何出現的句號、問號或驚嘆號。

在此正則運算式模式中,子模式 (\w+?) 的設計目的是要比對句子內的多個單字。 不過,物件的值 Group 只代表最後一個擷取的相符 (\w+?) 專案,而 Captures 屬性會 CaptureCollection 傳回 ,代表所有擷取的文字。 如輸出所示, CaptureCollection 第二個擷取群組的 包含四個 物件。 最後一個對應至 Group 物件。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"(\b(\w+?)[,:;]?\s?)+[?.!]";
      string input = "This is one sentence. This is a second sentence.";

      Match match = Regex.Match(input, pattern);
      Console.WriteLine("Match: " + match.Value);
      int groupCtr = 0;
      foreach (Group group in match.Groups)
      {
         groupCtr++;
         Console.WriteLine("   Group {0}: '{1}'", groupCtr, group.Value);
         int captureCtr = 0;
         foreach (Capture capture in group.Captures)
         {
            captureCtr++;
            Console.WriteLine("      Capture {0}: '{1}'", captureCtr, capture.Value);
         }
      }   
   }
}
// The example displays the following output:
//       Match: This is one sentence.
//          Group 1: 'This is one sentence.'
//             Capture 1: 'This is one sentence.'
//          Group 2: 'sentence'
//             Capture 1: 'This '
//             Capture 2: 'is '
//             Capture 3: 'one '
//             Capture 4: 'sentence'
//          Group 3: 'sentence'
//             Capture 1: 'This'
//             Capture 2: 'is'
//             Capture 3: 'one'
//             Capture 4: 'sentence'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "(\b(\w+?)[,:;]?\s?)+[?.!]"
      Dim input As String = "This is one sentence. This is a second sentence."

      Dim match As Match = Regex.Match(input, pattern)
      Console.WriteLine("Match: " + match.Value)
      Dim groupCtr As Integer = 0
      For Each group As Group In match.Groups
         groupCtr += 1
         Console.WriteLine("   Group {0}: '{1}'", groupCtr, group.Value)
         Dim captureCtr As Integer = 0
         For Each capture As Capture In group.Captures
            captureCtr += 1
            Console.WriteLine("      Capture {0}: '{1}'", captureCtr, capture.Value)
         Next
      Next   
   End Sub
End Module
' The example displays the following output:
'       Match: This is one sentence.
'          Group 1: 'This is one sentence.'
'             Capture 1: 'This is one sentence.'
'          Group 2: 'sentence'
'             Capture 1: 'This '
'             Capture 2: 'is '
'             Capture 3: 'one '
'             Capture 4: 'sentence'
'          Group 3: 'sentence'
'             Capture 1: 'This'
'             Capture 2: 'is'
'             Capture 3: 'one'
'             Capture 4: 'sentence'

屬性

Captures

依照最內層最左邊為優先的順序,取得符合擷取群組的所有擷取的集合 (如果規則運算式使用 RightToLeft 選項修改,則依照最內層最右邊為優先的順序), 集合可能有零個或更多項目。

Index

在原始字串中找到擷取的子字串的第一個字元之位置。

(繼承來源 Capture)
Length

取得所擷取子字串的長度。

(繼承來源 Capture)
Name

傳回目前執行個體所代表的擷取群組名稱。

Success

取得值,指出比對是否成功。

Value

從輸入字串取得擷取的子字串。

(繼承來源 Capture)
ValueSpan

從輸入字串取得擷取範圍。

(繼承來源 Capture)

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Synchronized(Group)

傳回 Group 物件,相等於可安全地在多個執行緒之間共用而提供的物件。

ToString()

擷取已透過呼叫 Value 屬性從輸入字串擷取的子字串。

(繼承來源 Capture)

適用於