CaptureCollection CaptureCollection CaptureCollection CaptureCollection Class

定义

表示一个捕获组做出的捕获的集合。Represents the set of captures made by a single capturing group.

public ref class CaptureCollection : System::Collections::ICollection
[System.Serializable]
public class CaptureCollection : System.Collections.ICollection
type CaptureCollection = class
    interface ICollection
    interface IEnumerable
Public Class CaptureCollection
Implements ICollection
继承
CaptureCollectionCaptureCollectionCaptureCollectionCaptureCollection
属性
实现

示例

下面的Capture示例比较Group.CapturesCaptureCollection 属性返回的对象中的对象。Match.CapturesThe following example compares the Capture objects in the CaptureCollection object returned by the Group.Captures and Match.Captures properties. 它还将Group对象CaptureGroup.Captures属性返回的CaptureCollection中的对象进行比较。It also compares Group objects with the Capture objects in the CaptureCollection returned by the Group.Captures property. 该示例使用以下两个正则表达式在单个输入字符串中查找匹配项:The example uses the following two regular expressions to find matches in a single input string:

  • \b\w+\W{1,2}

    此正则表达式模式标识包含一个或多个单词字符, 后跟一个或两个非单词字符 (如空格或标点符号) 的单词。This regular expression pattern identifies a word that consists of one or more word characters, followed by either one or two non-word characters such as white space or punctuation. 正则表达式不包括任何捕获组。The regular expression does not include any capturing groups. 该示例的Match输出显示Group.Captures , 和Match.Captures属性返回的对象CaptureCollection和对象都包含有关相同匹配项的信息。The output from the example shows that both the Match object and the CaptureCollection objects returned by the Group.Captures and Match.Captures properties contain information about the same match.

  • (\b\w+\W{1,2})+

    此正则表达式模式标识句子中的单词。This regular expression pattern identifies the words in a sentence. 模式定义单个捕获组, 其中包含一个或多个单词字符, 后跟一个或两个非单词字符。The pattern defines a single capturing group that consists of one or more word characters followed by one or two non-word characters. 正则表达式模式使用+限定符来匹配此组的一个或多个匹配项。The regular expression pattern uses the + quantifier to match one or more occurrences of this group. 此示例的输出显示Match , 对象CaptureCollectionMatch.Captures属性返回的对象包含有关相同匹配项的信息。The output from this example shows that the Match object and the CaptureCollection object returned by the Match.Captures property contain information about the same match. 第二Group个对象对应于正则表达式中的唯一捕获组, 只标识最后捕获的字符串, CaptureCollection而第一个捕获组的Group.Captures属性返回的对象包括所有捕获的子字符串。The second Group object, which corresponds to the only capturing group in the regular expression, identifies only the last captured string, whereas the CaptureCollection object returned by the first capturing group's Group.Captures property includes all captured substrings.

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern;  
      string input = "The young, hairy, and tall dog slowly walked across the yard.";
      Match match;

      // Match a word with a pattern that has no capturing groups.
      pattern = @"\b\w+\W{1,2}";
      match = Regex.Match(input, pattern);
      Console.WriteLine("Pattern: " + pattern);
      Console.WriteLine("Match: " + match.Value);
      Console.WriteLine("  Match.Captures: {0}", match.Captures.Count);
      for (int ctr = 0; ctr < match.Captures.Count; ctr++)
         Console.WriteLine("    {0}: '{1}'", ctr, match.Captures[ctr].Value);
      Console.WriteLine("  Match.Groups: {0}", match.Groups.Count);
      for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
      {
         Console.WriteLine("    Group {0}: '{1}'", 
                           groupCtr, match.Groups[groupCtr].Value);
         Console.WriteLine("    Group({0}).Captures: {1}", 
                           groupCtr, match.Groups[groupCtr].Captures.Count);
         for (int captureCtr = 0; captureCtr < match.Groups[groupCtr].Captures.Count; captureCtr++)
            Console.WriteLine("      Capture {0}: '{1}'", 
                              captureCtr, 
                              match.Groups[groupCtr].Captures[captureCtr].Value);
      }
      Console.WriteLine("-----\n");

      // Match a sentence with a pattern that has a quantifier that 
      // applies to the entire group.
      pattern = @"(\b\w+\W{1,2})+";
      match = Regex.Match(input, pattern);
      Console.WriteLine("Pattern: " + pattern);
      Console.WriteLine("Match: " + match.Value);
      Console.WriteLine("  Match.Captures: {0}", match.Captures.Count);
      for (int ctr = 0; ctr < match.Captures.Count; ctr++)
         Console.WriteLine("    {0}: '{1}'", ctr, match.Captures[ctr].Value);
      
      Console.WriteLine("  Match.Groups: {0}", match.Groups.Count);
      for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
      {
         Console.WriteLine("    Group {0}: '{1}'", groupCtr, match.Groups[groupCtr].Value);
         Console.WriteLine("    Group({0}).Captures: {1}", 
                           groupCtr, match.Groups[groupCtr].Captures.Count);
         for (int captureCtr = 0; captureCtr < match.Groups[groupCtr].Captures.Count; captureCtr++)
            Console.WriteLine("      Capture {0}: '{1}'", captureCtr, match.Groups[groupCtr].Captures[captureCtr].Value);
      }
   }
}
// The example displays the following output:
//    Pattern: \b\w+\W{1,2}
//    Match: The
//      Match.Captures: 1
//        0: 'The '
//      Match.Groups: 1
//        Group 0: 'The '
//        Group(0).Captures: 1
//          Capture 0: 'The '
//    -----
//    
//    Pattern: (\b\w+\W{1,2})+
//    Match: The young, hairy, and tall dog slowly walked across the yard.
//      Match.Captures: 1
//        0: 'The young, hairy, and tall dog slowly walked across the yard.'
//      Match.Groups: 2
//        Group 0: 'The young, hairy, and tall dog slowly walked across the yard.'
//        Group(0).Captures: 1
//          Capture 0: 'The young, hairy, and tall dog slowly walked across the yard.'
//        Group 1: 'yard.'
//        Group(1).Captures: 11
//          Capture 0: 'The '
//          Capture 1: 'young, '
//          Capture 2: 'hairy, '
//          Capture 3: 'and '
//          Capture 4: 'tall '
//          Capture 5: 'dog '
//          Capture 6: 'slowly '
//          Capture 7: 'walked '
//          Capture 8: 'across '
//          Capture 9: 'the '
//          Capture 10: 'yard.'
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String  
      Dim input As String = "The young, hairy, and tall dog slowly walked across the yard."
      Dim match As Match

      ' Match a word with a pattern that has no capturing groups.
      pattern = "\b\w+\W{1,2}"
      match = Regex.Match(input, pattern)
      Console.WriteLine("Pattern: " + pattern)
      Console.WriteLine("Match: " + match.Value)
      Console.WriteLine("  Match.Captures: {0}", match.Captures.Count)
      For ctr As Integer = 0 To match.Captures.Count - 1
         Console.WriteLine("    {0}: '{1}'", ctr, match.Captures(ctr).Value)
      Next
      Console.WriteLine("  Match.Groups: {0}", match.Groups.Count)
      For groupCtr As Integer = 0 To match.Groups.Count - 1
         Console.WriteLine("    Group {0}: '{1}'", groupCtr, match.Groups(groupCtr).Value)
         Console.WriteLine("    Group({0}).Captures: {1}", _
                           groupCtr, match.Groups(groupCtr).Captures.Count)
         For captureCtr As Integer = 0 To match.Groups(groupCtr).Captures.Count - 1
            Console.WriteLine("      Capture {0}: '{1}'", _
                              captureCtr, _
                              match.Groups(groupCtr).Captures(captureCtr).Value)
         Next
      Next
      Console.WriteLine("-----")
      Console.WriteLine()

      ' Match a sentence with a pattern that has a quantifier that 
      ' applies to the entire group.
      pattern = "(\b\w+\W{1,2})+"
      match = Regex.Match(input, pattern)
      Console.WriteLine("Pattern: " + pattern)
      Console.WriteLine("Match: " + match.Value)
      Console.WriteLine("  Match.Captures: {0}", match.Captures.Count)
      For ctr As Integer = 0 To match.Captures.Count - 1
         Console.WriteLine("    {0}: '{1}'", ctr, match.Captures(ctr).Value)
      Next
      Console.WriteLine("  Match.Groups: {0}", match.Groups.Count)
      For groupCtr As Integer = 0 To match.Groups.Count - 1
         Console.WriteLine("    Group {0}: '{1}'", groupCtr, match.Groups(groupCtr).Value)
         Console.WriteLine("    Group({0}).Captures: {1}", _
                           groupCtr, match.Groups(groupCtr).Captures.Count)
         For captureCtr As Integer = 0 To match.Groups(groupCtr).Captures.Count - 1
            Console.WriteLine("      Capture {0}: '{1}'", captureCtr, match.Groups(groupCtr).Captures(captureCtr).Value)
         Next
      Next
   End Sub
End Module
' The example displays the following output:
'    Pattern: \b\w+\W{1,2}
'    Match: The
'      Match.Captures: 1
'        0: 'The '
'      Match.Groups: 1
'        Group 0: 'The '
'        Group(0).Captures: 1
'          Capture 0: 'The '
'    -----
'    
'    Pattern: (\b\w+\W{1,2})+
'    Match: The young, hairy, and tall dog slowly walked across the yard.
'      Match.Captures: 1
'        0: 'The young, hairy, and tall dog slowly walked across the yard.'
'      Match.Groups: 2
'        Group 0: 'The young, hairy, and tall dog slowly walked across the yard.'
'        Group(0).Captures: 1
'          Capture 0: 'The young, hairy, and tall dog slowly walked across the yard.'
'        Group 1: 'yard.'
'        Group(1).Captures: 11
'          Capture 0: 'The '
'          Capture 1: 'young, '
'          Capture 2: 'hairy, '
'          Capture 3: 'and '
'          Capture 4: 'tall '
'          Capture 5: 'dog '
'          Capture 6: 'slowly '
'          Capture 7: 'walked '
'          Capture 8: 'across '
'          Capture 9: 'the '
'          Capture 10: 'yard.'

注解

该集合是不可变的 (只读), 没有公共构造函数。The collection is immutable (read-only) and has no public constructor. 对象包含一个或多个Capture对象。 CaptureCollectionThe CaptureCollection object contains one or more Capture objects.

CaptureCollection类的实例由以下属性返回:Instances of the CaptureCollection class are returned by the following properties:

  • Group.Captures 属性。The Group.Captures property. 集合中的每个成员都表示捕获组捕获的子字符串。Each member of the collection represents a substring captured by a capturing group. 如果未将限定符应用于捕获组, 则CaptureCollection包含Capture一个对象, 该对象表示与Group对象相同的捕获子字符串。If a quantifier is not applied to a capturing group, the CaptureCollection includes a single Capture object that represents the same captured substring as the Group object. 如果将限定符应用于捕获组, 则对捕获CaptureCollection的每Capture个子字符串都包含一个对象, 并且Group该对象仅提供有关最后捕获的子字符串的信息。If a quantifier is applied to a capturing group, the CaptureCollection includes one Capture object for each captured substring, and the Group object provides information only about the last captured substring.

  • Match.Captures 属性。The Match.Captures property. 在这种情况下, 集合包含Capture一个对象, 该对象提供有关整个匹配项的信息。In this case, the collection consists of a single Capture object that provides information about the match as a whole. 也就是说, CaptureCollection对象提供Match与对象相同的信息。That is, the CaptureCollection object provides the same information as the Match object.

若要循环访问集合的成员, 应使用语言提供的集合迭代构造 (如foreach For Each C#和 ...NextTo iterate through the members of the collection, you should use the collection iteration construct provided by your language (such as foreach in C# and For EachNext 在 Visual Basic 中), 而不是检索GetEnumerator方法返回的枚举器。in Visual Basic) instead of retrieving the enumerator that is returned by the GetEnumerator method.

属性

Count Count Count Count

获取由该组捕获的子字符串数。Gets the number of substrings captured by the group.

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

获取一个值,指示集合是否为只读集合。Gets a value that indicates whether the collection is read only.

IsSynchronized IsSynchronized IsSynchronized IsSynchronized

获取一个值,该值指示对集合的访问是否同步(线程安全)。Gets a value that indicates whether access to the collection is synchronized (thread-safe).

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

获取该集合的单个成员。Gets an individual member of the collection.

SyncRoot SyncRoot SyncRoot SyncRoot

获取可用于同步对集合的访问的对象。Gets an object that can be used to synchronize access to the collection.

方法

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

将集合的所有元素复制到给定数组中(从给定索引处开始)。Copies all the elements of the collection to the given array beginning at the given index.

CopyTo(Capture[], Int32) CopyTo(Capture[], Int32) CopyTo(Capture[], Int32) CopyTo(Capture[], Int32)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

提供一个循环访问集合的枚举器。Provides an enumerator that iterates through the collection.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

显式界面实现

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

将集合的所有元素复制到给定数组中(从给定索引处开始)。Copies all the elements of the collection to the given array beginning at the given index.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

获取一个值,该值指示对集合的访问是否同步(线程安全)。Gets a value that indicates whether access to the collection is synchronized (thread-safe).

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

获取可用于同步对集合的访问的对象。Gets an object that can be used to synchronize access to the collection.

ICollection<Capture>.Add(Capture) ICollection<Capture>.Add(Capture) ICollection<Capture>.Add(Capture) ICollection<Capture>.Add(Capture)
ICollection<Capture>.Clear() ICollection<Capture>.Clear() ICollection<Capture>.Clear() ICollection<Capture>.Clear()
ICollection<Capture>.Contains(Capture) ICollection<Capture>.Contains(Capture) ICollection<Capture>.Contains(Capture) ICollection<Capture>.Contains(Capture)
ICollection<Capture>.Remove(Capture) ICollection<Capture>.Remove(Capture) ICollection<Capture>.Remove(Capture) ICollection<Capture>.Remove(Capture)
IEnumerable<Capture>.GetEnumerator() IEnumerable<Capture>.GetEnumerator() IEnumerable<Capture>.GetEnumerator() IEnumerable<Capture>.GetEnumerator()
IList.Add(Object) IList.Add(Object) IList.Add(Object) IList.Add(Object)
IList.Clear() IList.Clear() IList.Clear() IList.Clear()
IList.Contains(Object) IList.Contains(Object) IList.Contains(Object) IList.Contains(Object)
IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object)
IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object)
IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize
IList.Item[Int32] IList.Item[Int32] IList.Item[Int32] IList.Item[Int32]
IList.Remove(Object) IList.Remove(Object) IList.Remove(Object) IList.Remove(Object)
IList.RemoveAt(Int32) IList.RemoveAt(Int32) IList.RemoveAt(Int32) IList.RemoveAt(Int32)
IList<Capture>.IndexOf(Capture) IList<Capture>.IndexOf(Capture) IList<Capture>.IndexOf(Capture) IList<Capture>.IndexOf(Capture)
IList<Capture>.Insert(Int32, Capture) IList<Capture>.Insert(Int32, Capture) IList<Capture>.Insert(Int32, Capture) IList<Capture>.Insert(Int32, Capture)
IList<Capture>.Item[Int32] IList<Capture>.Item[Int32] IList<Capture>.Item[Int32] IList<Capture>.Item[Int32]
IList<Capture>.RemoveAt(Int32) IList<Capture>.RemoveAt(Int32) IList<Capture>.RemoveAt(Int32) IList<Capture>.RemoveAt(Int32)

扩展方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的元素强制转换为指定的类型。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根据指定类型筛选 IEnumerable 的元素。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

启用查询的并行化。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 转换为 IQueryableConverts an IEnumerable to an IQueryable.

适用于

另请参阅