通过


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 语言元素。
[,:;]? 匹配逗号、冒号或分号的零个或一个匹配项。
\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)

适用于