Export (0) Print
Expand All

CaptureCollection Class

Updated: February 2010

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

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

[SerializableAttribute]
public ref class CaptureCollection : ICollection, 
	IEnumerable

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.

No code example is currently available or this language may not be supported.

System::Object
  System.Text.RegularExpressions::CaptureCollection

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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

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

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

February 2010

Revised extensively.

Information enhancement.

Community Additions

ADD
Show:
© 2014 Microsoft