Group.Captures 属性

定义

按从里到外、从左到右的顺序获取由捕获组匹配的所有捕获的集合(如果正则表达式用 RightToLeft 选项修改了,则顺序为按从里到外、从右到左)。 该集合可以有零个或更多的项。

public:
 property System::Text::RegularExpressions::CaptureCollection ^ Captures { System::Text::RegularExpressions::CaptureCollection ^ get(); };
public System.Text.RegularExpressions.CaptureCollection Captures { get; }
member this.Captures : System.Text.RegularExpressions.CaptureCollection
Public ReadOnly Property Captures As CaptureCollection

属性值

CaptureCollection

由该组匹配的子字符串的集合。

注解

如果限定符未应用于捕获组,则属性返回的 Captures 集合包含一 Capture 个对象,该对象提供有关与对象相同的子字符串 Group 的信息。 下列示例对此进行了阐释。 它定义正则表达式, \b(\w+)\b用于从句子中提取单个单词。 该 Group 对象捕获“This”一词,并且包含有关同一捕获的信息的单个对象 CaptureCollection

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b(\w+)\b";
      string input = "This is one sentence.";
      Match match = Regex.Match(input, pattern);
      if (match.Success) {
         Console.WriteLine("Matched text: {0}", match.Value);
         for (int ctr = 1; ctr <= match.Groups.Count - 1; ctr++) {
            Console.WriteLine("   Group {0}:  {1}", ctr, match.Groups[ctr].Value);
            int captureCtr = 0;
            foreach (Capture capture in match.Groups[ctr].Captures) {
               Console.WriteLine("      Capture {0}: {1}", 
                                 captureCtr, capture.Value);
               captureCtr += 1;                  
            }
         }
      }   
   }
}
// The example displays the following output:
//       Matched text: This
//          Group 1:  This
//             Capture 0: This
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b(\w+)\b"
      Dim input As String = "This is one sentence."
      Dim match As Match = Regex.Match(input, pattern)
      If match.Success Then
         Console.WriteLine("Matched text: {0}", match.Value)
         For ctr As Integer = 1 To match.Groups.Count - 1
            Console.WriteLine("   Group {0}:  {1}", ctr, match.Groups(ctr).Value)
            Dim captureCtr As Integer = 0
            For Each capture As Capture In match.Groups(ctr).Captures
               Console.WriteLine("      Capture {0}: {1}", _
                                 captureCtr, capture.Value)
               captureCtr += 1                  
            Next
         Next
      End If   
   End Sub
End Module
' The example displays the following output:
'       Matched text: This
'          Group 1:  This
'             Capture 0: This

当限定符应用于捕获组,以便该组在单个正则表达式中捕获多个子字符串时,将发生该属性的实际实用工具 Captures 。 在这种情况下,该 Group 对象包含有关上次捕获的子字符串的信息,而 Captures 该属性包含有关组捕获的所有子字符串的信息。 在以下示例中,正则表达式 \b(\w+\s*)+\. 与句点结束的整个句子匹配。 (\w+\s*)+组捕获集合中的单个单词。 Group由于集合仅包含有关上次捕获的子字符串的信息,因此它会捕获句子“句子”中的最后一个单词。 但是,组捕获的每个单词都可以从属性返回的 Captures 集合中获取。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "This is a sentence. This is another sentence.";
      string pattern = @"\b(\w+\s*)+\.";
      Match match = Regex.Match(input, pattern);
      if (match.Success) {
         Console.WriteLine("Matched text: {0}", match.Value);
         for (int ctr = 1; ctr < match.Groups.Count; ctr++) {
            Console.WriteLine("   Group {0}:  {1}", ctr, match.Groups[ctr].Value);
            int captureCtr = 0;
            foreach (Capture capture in match.Groups[ctr].Captures) {
               Console.WriteLine("      Capture {0}: {1}", 
                                 captureCtr, capture.Value);
               captureCtr++; 
            }
         }
      }   
   }
}
// The example displays the following output:
//       Matched text: This is a sentence.
//          Group 1:  sentence
//             Capture 0: This
//             Capture 1: is
//             Capture 2: a
//             Capture 3: sentence
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "This is a sentence. This is another sentence."
      Dim pattern As String = "\b(\w+\s*)+\."
      Dim match As Match = Regex.Match(input, pattern)
      If match.Success Then
         Console.WriteLine("Matched text: {0}", match.Value)
         For ctr As Integer = 1 To match.Groups.Count - 1
            Console.WriteLine("   Group {0}:  {1}", ctr, match.Groups(ctr).Value)
            Dim captureCtr As Integer = 0
            For Each capture As Capture In match.Groups(ctr).Captures
               Console.WriteLine("      Capture {0}: {1}", _
                                 captureCtr, capture.Value)
               captureCtr += 1                  
            Next
         Next
      End If   
   End Sub
End Module
' The example displays the following output:
'       Matched text: This is a sentence.
'          Group 1:  sentence
'             Capture 0: This
'             Capture 1: is
'             Capture 2: a
'             Capture 3: sentence

适用于

另请参阅