Group 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示来自单个捕获组的结果。
public ref class Group : System::Text::RegularExpressions::Capturepublic class Group : System.Text.RegularExpressions.Capture[System.Serializable]
public class Group : System.Text.RegularExpressions.Capturetype Group = class
    inherit Capture[<System.Serializable>]
type Group = class
    inherit CapturePublic Class Group
Inherits Capture- 继承
- 派生
- 属性
注解
由于限定符,捕获组可以在单个匹配中捕获零个、一个或多个字符串。  (有关详细信息,请参阅 限定符.) 属性中提供了 Group.Captures 与单个捕获组匹配的所有子字符串。 可以直接从属性Index访问Value捕获的最后一个子字符串的信息。  (也就是说, Group 实例等效于属性返回 Captures 的集合的最后一项,这反映了捕获 group.)
一个示例有助于阐明 Group 对象与 System.Text.RegularExpressions.CaptureCollection 属性返回 Captures 的关系。 正则表达式模式 (\b(\w+?)[,:;]?\s?)+[?.!] 与整个句子匹配。 该正则表达式的定义如下表所示。
| 模式 | 描述 | 
|---|---|
| \b | 在单词边界处开始匹配。 | 
| (\w+?) | 匹配一个或多个单词字符,但字符要尽可能的少。 这是第二个 (内部) 捕获组。  (第一个捕获组包括 \b语言元素.) | 
| [,:;]? | 匹配逗号、冒号或分号的零个或一个匹配项。 | 
| \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) | 返回一个与提供的对象等效的  | 
| ToString() | 通过调用 Value 属性,从输入的字符串中检索捕获的子字符串。(继承自 Capture) |