Group 類別


代表單一擷取群組的結果。Represents the results from a single capturing group.

public ref class Group : System::Text::RegularExpressions::Capture
public class Group : System.Text.RegularExpressions.Capture
type Group = class
    inherit Capture
Public Class Group
Inherits Capture


由於數量詞的關係,捕捉群組可以在單一相符項中捕捉零個、一個或多個字串。A capturing group can capture zero, one, or more strings in a single match because of quantifiers. (如需詳細資訊,請參閱數量值)。單一「捕獲群組」所符合的所有子字串都可從 Group.Captures 屬性取得。(For more information, see Quantifiers.) All the substrings matched by a single capturing group are available from the Group.Captures property. 最後一個已捕捉的子字串的相關資訊可以直接從 ValueIndex 屬性存取。Information about the last substring captured can be accessed directly from the Value and Index properties. (也就是說,Group 實例相當於 Captures 屬性所傳回之集合的最後一個專案,它會反映由捕捉群組所進行的最後一個捕獲)。(That is, the Group instance is equivalent to the last item of the collection returned by the Captures property, which reflects the last capture made by the capturing group.)

範例有助於清楚說明 Group 物件與 Captures 屬性傳回的 System.Text.RegularExpressions.CaptureCollection 之間的關聯性。An example helps to clarify this relationship between a Group object and the System.Text.RegularExpressions.CaptureCollection that is returned by the Captures property. 正則運算式模式 (\b(\w+?)[,:;]?\s?)+[?.!] 符合整個句子。The regular expression pattern (\b(\w+?)[,:;]?\s?)+[?.!] matches entire sentences. 規則運算式的定義如下表所示。The regular expression is defined as shown in the following table.

模式Pattern 描述Description
\b 開始字緣比對。Begin the match at a word boundary.
(\w+?) 比對一或多個字元,但字元數愈少愈好。Match one or more word characters, but as few characters as possible. 這是第二個(內部)的「捕獲群組」。This is the second (inner) capturing group. (第一個捕獲群組包含 \b 語言元素)。(The first capturing group includes the \b language element.)
[,:;]? 比對出現零或一次的逗號、冒號或分號。Match zero or one occurrence of a comma, colon, or semicolon.
\s? 比對零或一次出現的空白字元。Match zero or one occurrence of a white-space character.
(\b(\w+?)[,:;]?\s?)+ 比對包含一個字邊界的模式、一個或多個文字字元、一個標點符號,以及一個或多次的空白字元。Match the pattern consisting of a word boundary, one or more word characters, a punctuation symbol, and a white-space character one or more times. 這是第一個擷取群組。This is the first capturing group.
[?.!] 符合任何出現的句點、問號或驚嘆號。Match any occurrence of a period, question mark, or exclamation point.

在此正則運算式模式中,子模式 (\w+?) 是設計來比對句子中的多個單字。In this regular expression pattern, the subpattern (\w+?) is designed to match multiple words within a sentence. 不過,Group 物件的值只代表 (\w+?) 捕獲的最後一個相符項,而 Captures 屬性則會傳回代表所有已捕捉之文字的 CaptureCollectionHowever, the value of the Group object represents only the last match that (\w+?) captures, whereas the Captures property returns a CaptureCollection that represents all captured text. 如輸出所示,第二個捕獲群組的 CaptureCollection 包含四個物件。As the output shows, the CaptureCollection for the second capturing group contains four objects. 最後一個會對應至 Group 物件。The last of these corresponds to the Group object.

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)
         Console.WriteLine("   Group {0}: '{1}'", groupCtr, group.Value);
         int captureCtr = 0;
         foreach (Capture capture in group.Captures)
            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)
   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'



依照最內層最左邊為優先的順序,取得符合擷取群組的所有擷取的集合 (如果規則運算式使用 RightToLeft 選項修改,則依照最內層最右邊為優先的順序),Gets a collection of all the captures matched by the capturing group, in innermost-leftmost-first order (or innermost-rightmost-first order if the regular expression is modified with the RightToLeft option). 集合可能有零個或更多項目。The collection may have zero or more items.


在原始字串中找到擷取的子字串的第一個字元之位置。The position in the original string where the first character of the captured substring is found.

(繼承來源 Capture)

取得所擷取子字串的長度。Gets the length of the captured substring.

(繼承來源 Capture)

傳回目前執行個體所代表的擷取群組名稱。Returns the name of the capturing group represented by the current instance.


取得值,指出比對是否成功。Gets a value indicating whether the match is successful.


從輸入字串取得擷取的子字串。Gets the captured substring from the input string.

(繼承來源 Capture)



判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)

做為預設雜湊函式。Serves as the default hash function.

(繼承來源 Object)

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)

傳回 Group 物件,相等於可安全地在多個執行緒之間共用而提供的物件。Returns a Group object equivalent to the one supplied that is safe to share between multiple threads.


擷取已透過呼叫 Value 屬性從輸入字串擷取的子字串。Retrieves the captured substring from the input string by calling the Value property.

(繼承來源 Capture)