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)

'Declaration
<SerializableAttribute> _
Public Class CaptureCollection _
	Implements ICollection, IEnumerable

The CaptureCollection type exposes the following members.

  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCountGets the number of substrings captured by the group.
Public propertySupported by the XNA FrameworkIsReadOnlyGets a value that indicates whether the collection is read only.
Public propertySupported by the XNA FrameworkIsSynchronizedGets a value that indicates whether access to the collection is synchronized (thread-safe).
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsItemGets an individual member of the collection.
Public propertySupported by the XNA FrameworkSyncRootGets an object that can be used to synchronize access to the collection.
Top

  NameDescription
Public methodSupported by the XNA FrameworkCopyToCopies all the elements of the collection to the given array beginning at the given index.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetEnumeratorProvides an enumerator that iterates through the collection.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Public Extension MethodSupported by Portable Class LibrarySupported in .NET for Windows Store appsAsParallelEnables parallelization of a query. (Defined by ParallelEnumerable.)
Public Extension MethodSupported by Portable Class LibrarySupported in .NET for Windows Store appsAsQueryableConverts an IEnumerable to an IQueryable. (Defined by Queryable.)
Public Extension MethodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsCast(Of TResult)Casts the elements of an IEnumerable to the specified type. (Defined by Enumerable.)
Public Extension MethodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsOfType(Of TResult)Filters the elements of an IEnumerable based on a specified type. (Defined by Enumerable.)
Top

  NameDescription
Explicit interface implemetationPrivate methodSupported by Portable Class LibrarySupported in .NET for Windows Store appsICollection.CopyToCopies all the elements of the collection to the given array beginning at the given index.
Explicit interface implemetationPrivate propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsICollection.IsSynchronizedGets a value that indicates whether access to the collection is synchronized (thread-safe).
Explicit interface implemetationPrivate propertySupported by Portable Class LibrarySupported in .NET for Windows Store appsICollection.SyncRootGets an object that can be used to synchronize access to the collection.
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.

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

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft