Поделиться через


CaptureCollection Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Represents the set of captures made by a single capturing group.

Inheritance Hierarchy

System.Object
  System.Text.RegularExpressions.CaptureCollection

Namespace:  System.Text.RegularExpressions
Assembly:  System (in System.dll)

Syntax

'Declaration
<DefaultMemberAttribute("Item")> _
Public Class CaptureCollection _
    Implements ICollection, IEnumerable
[DefaultMemberAttribute("Item")]
public class CaptureCollection : ICollection, 
    IEnumerable

The CaptureCollection type exposes the following members.

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Count Gets the number of substrings captured by the group.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsReadOnly Gets a value that indicates whether the collection is read-only.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 IsSynchronized Gets a value that indicates whether access to the collection is synchronized (thread-safe).
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Item Gets an individual member of the collection.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 SyncRoot Gets an object that can be used to synchronize access to the collection.

Top

Methods

  Name Description
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 CopyTo Copies all the elements of the collection to the given array beginning at the given index.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals(Object) Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Finalize Allows an object to try to free resources and perform other cleanup operations before the Object is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetEnumerator Provides an enumerator that iterates through the collection.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetHashCode Serves as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetType Gets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360 ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Extension Methods

  Name Description
Public Extension MethodSupported by Silverlight for Windows Phone AsQueryable Converts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodSupported by Silverlight for Windows PhoneSupported by Xbox 360 Cast<TResult> Converts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodSupported by Silverlight for Windows PhoneSupported by Xbox 360 OfType<TResult> Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)

Top

Remarks

The collection is immutable (read-only) and has no public constructor. The CaptureCollection object contains one or more Capture objects.

Instances of the CaptureCollection class are returned by the following properties:

  • The Group.Captures property. Each member of the collection represents a substring captured by a capturing 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. 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.

  • The Match.Captures property. In this case, the collection consists of a single Capture object that provides information about the match as a whole. That is, the CaptureCollection object provides the same information as the Match object.

To 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 Each…Next in Visual Basic) instead of retrieving the enumerator that is returned by the GetEnumerator method.

Examples

The following example compares the Capture objects in the CaptureCollection object returned by the Group.Captures and Match.Captures properties. 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. 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. 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. 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.

Imports System.Text.RegularExpressions

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      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)
      outputBlock.Text &= "Pattern: " + pattern + vbCrLf
      outputBlock.Text &= "Match: " + match.Value + vbCrLf
      outputBlock.Text += String.Format("  Match.Captures: {0}", match.Captures.Count) + vbCrLf
      For ctr As Integer = 0 To match.Captures.Count - 1
         outputBlock.Text += String.Format("    {0}: '{1}'", ctr, match.Captures(ctr).Value) + vbCrLf
      Next
      outputBlock.Text += String.Format("  Match.Groups: {0}", match.Groups.Count) + vbCrLf
      For groupCtr As Integer = 0 To match.Groups.Count - 1
         outputBlock.Text += String.Format("    Group {0}: '{1}'", groupCtr, match.Groups(groupCtr).Value) + vbCrLf
         outputBlock.Text += String.Format("    Group({0}).Captures: {1}", _
                           groupCtr, match.Groups(groupCtr).Captures.Count) + vbCrLf
         For captureCtr As Integer = 0 To match.Groups(groupCtr).Captures.Count - 1
            outputBlock.Text += String.Format("      Capture {0}: '{1}'", _
                              captureCtr, _
                              match.Groups(groupCtr).Captures(captureCtr).Value) + vbCrLf
         Next
      Next
      outputBlock.Text &= "-----" + vbCrLf
      outputBlock.Text &= vbCrLf

      ' 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)
      outputBlock.Text &= "Pattern: " + pattern + vbCrLf
      outputBlock.Text &= "Match: " + match.Value + vbCrLf
      outputBlock.Text += String.Format("  Match.Captures: {0}", match.Captures.Count) + vbCrLf
      For ctr As Integer = 0 To match.Captures.Count - 1
         outputBlock.Text += String.Format("    {0}: '{1}'", ctr, match.Captures(ctr).Value) + vbCrLf
      Next
      outputBlock.Text += String.Format("  Match.Groups: {0}", match.Groups.Count) + vbCrLf
      For groupCtr As Integer = 0 To match.Groups.Count - 1
         outputBlock.Text += String.Format("    Group {0}: '{1}'", groupCtr, match.Groups(groupCtr).Value) + vbCrLf
         outputBlock.Text += String.Format("    Group({0}).Captures: {1}", _
                           groupCtr, match.Groups(groupCtr).Captures.Count) + vbCrLf
         For captureCtr As Integer = 0 To match.Groups(groupCtr).Captures.Count - 1
            outputBlock.Text += String.Format("      Capture {0}: '{1}'", captureCtr, match.Groups(groupCtr).Captures(captureCtr).Value) + vbCrLf
         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.'
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      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);
      outputBlock.Text += "Pattern: " + pattern + "\n";
      outputBlock.Text += "Match: " + match.Value + "\n";
      outputBlock.Text += String.Format("  Match.Captures: {0}\n", match.Captures.Count);
      for (int ctr = 0; ctr < match.Captures.Count; ctr++)
         outputBlock.Text += String.Format("    {0}: '{1}'", ctr, match.Captures[ctr].Value) + "\n";
      outputBlock.Text += String.Format("  Match.Groups: {0}", match.Groups.Count) + "\n";
      for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
      {
         outputBlock.Text += String.Format("    Group {0}: '{1}'",
                           groupCtr, match.Groups[groupCtr].Value) + "\n";
         outputBlock.Text += String.Format("    Group({0}).Captures: {1}",
                           groupCtr, match.Groups[groupCtr].Captures.Count) + "\n";
         for (int captureCtr = 0; captureCtr < match.Groups[groupCtr].Captures.Count; captureCtr++)
            outputBlock.Text += String.Format("      Capture {0}: '{1}'",
                              captureCtr,
                              match.Groups[groupCtr].Captures[captureCtr].Value) + "\n";
      }
      outputBlock.Text += "-----\n" + "\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);
      outputBlock.Text += "Pattern: " + pattern + "\n";
      outputBlock.Text += "Match: " + match.Value + "\n";
      outputBlock.Text += String.Format("  Match.Captures: {0}\n", match.Captures.Count);
      for (int ctr = 0; ctr < match.Captures.Count; ctr++)
         outputBlock.Text += String.Format("    {0}: '{1}'\n", ctr, match.Captures[ctr].Value);

      outputBlock.Text += String.Format("  Match.Groups: {0}", match.Groups.Count) + "\n";
      for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
      {
         outputBlock.Text += String.Format("    Group {0}: '{1}'", groupCtr, match.Groups[groupCtr].Value) + "\n";
         outputBlock.Text += String.Format("    Group({0}).Captures: {1}",
                           groupCtr, match.Groups[groupCtr].Captures.Count) + "\n";
         for (int captureCtr = 0; captureCtr < match.Groups[groupCtr].Captures.Count; captureCtr++)
            outputBlock.Text += String.Format("      Capture {0}: '{1}'", captureCtr, match.Groups[groupCtr].Captures[captureCtr].Value) + "\n";
      }
   }
}
// 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.'

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.