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对象System.Text.RegularExpressions.CaptureCollectionCaptures属性返回的之间的关系。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+?)捕获的最后一个匹配项, 而Captures属性返回一个CaptureCollection表示所有捕获的文本的。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)
         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)