Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

CaptureCollection Class

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

System.Object
  System.Text.RegularExpressions.CaptureCollection

Namespace:  System.Text.RegularExpressions
Assembly:  System (in System.dll)
[DefaultMemberAttribute("Item")]
public class CaptureCollection : ICollection, 
	IEnumerable

The CaptureCollection type exposes the following members.

  NameDescription
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360CountGets the number of substrings captured by the group.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsReadOnlyGets a value that indicates whether the collection is read-only.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360IsSynchronizedGets a value that indicates whether access to the collection is synchronized (thread-safe).
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360ItemGets an individual member of the collection.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360SyncRootGets an object that can be used to synchronize access to the collection.
Top
  NameDescription
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360CopyToCopies all the elements of the collection to the given array beginning at the given index.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360Equals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360FinalizeAllows 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 360GetEnumeratorProvides an enumerator that iterates through the collection.
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360GetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by Silverlight for Windows PhoneSupported by Xbox 360MemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by Silverlight for Windows PhoneSupported by Xbox 360ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
  NameDescription
Public Extension MethodSupported by Silverlight for Windows PhoneAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodSupported by Silverlight for Windows PhoneSupported by Xbox 360Cast<TResult>Converts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodSupported by Silverlight for Windows PhoneSupported by Xbox 360OfType<TResult>Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top

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

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.


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.'


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

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

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.