Group 類別

定義

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

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

備註

由於數量詞的關係, 捕捉群組可以在單一相符項中捕捉零個、一個或多個字串。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 language 元素)。(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+?)相符項, CaptureCollectionCaptures屬性會傳回代表所有已捕捉文字的。However, 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)
      {
         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 選項修改,則依照最內層最右邊為優先的順序),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.

Index

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

(繼承來源 Capture)
Length

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

(繼承來源 Capture)
Name

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

Success

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

Value

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

(繼承來源 Capture)

方法

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

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

(繼承來源 Object)
MemberwiseClone()

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

(繼承來源 Object)
Synchronized(Group)

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

ToString()

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

(繼承來源 Capture)

適用於