共用方式為


Capture Class

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

Updated: August 2009

Represents the results from a single subexpression capture.

Inheritance Hierarchy

System.Object
  System.Text.RegularExpressions.Capture
    System.Text.RegularExpressions.Group

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

Syntax

'Declaration
Public Class Capture
public class Capture

The Capture type exposes the following members.

Properties

  Name Description
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Index The position in the original string where the first character of the captured substring was found.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Length The length of the captured substring.
Public propertySupported by Silverlight for Windows PhoneSupported by Xbox 360 Value Gets the captured substring from the input string.

Top

Methods

  Name Description
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 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 Gets the captured substring from the input string. (Overrides Object.ToString().)

Top

Remarks

A Capture object is immutable and has no public constructor. Instances are returned through the CaptureCollection object, which is returned by the Match.Captures and Group.Captures properties. However, the Match.Captures property provides information about the same match as the Match object.

If you do not apply a quantifier to a capturing group, the Group.Captures property returns a CaptureCollection with a single Capture object that provides information about the same capture as the Group object. If you do apply a quantifier to a capturing group, the Group.Index, Group.Length, and Group.Value properties provide information only about the last captured group, whereas the Capture objects in the CaptureCollection provide information about all subexpression captures. The example provides an illustration.

Examples

The following example defines a regular expression that matches sentences that contain no punctuation except for a period (".").

Imports System.Text.RegularExpressions

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim input As String = "Yes. This dog is very friendly."
      Dim pattern As String = "((\w+)[\s.])+"
      For Each match As Match In Regex.Matches(input, pattern)
         outputBlock.Text += String.Format("Match: {0}", match.Value) & vbCrLf
         For groupCtr As Integer = 0 To match.Groups.Count - 1
            Dim group As Group = match.Groups(groupCtr)
            outputBlock.Text += String.Format("   Group {0}: {1}", groupCtr, group.Value) & vbCrLf
            For captureCtr As Integer = 0 To group.Captures.Count - 1
               outputBlock.Text += String.Format("      Capture {0}: {1}", captureCtr, _
                                 group.Captures(captureCtr).Value) & vbCrLf
            Next
         Next
      Next
   End Sub
End Module
' The example displays the following output:
'       Match: Yes.
'          Group 0: Yes.
'             Capture 0: Yes.
'          Group 1: Yes.
'             Capture 0: Yes.
'          Group 2: Yes
'             Capture 0: Yes
'       Match: This dog is very friendly.
'          Group 0: This dog is very friendly.
'             Capture 0: This dog is very friendly.
'          Group 1: friendly.
'             Capture 0: This
'             Capture 1: dog
'             Capture 2: is
'             Capture 3: very
'             Capture 4: friendly.
'          Group 2: friendly
'             Capture 0: This
'             Capture 1: dog
'             Capture 2: is
'             Capture 3: very
'             Capture 4: friendly
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string input = "Yes. This dog is very friendly.";
      string pattern = @"((\w+)[\s.])+";
      foreach (Match match in Regex.Matches(input, pattern))
      {
         outputBlock.Text += String.Format("Match: {0}", match.Value) + "\n";
         for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
         {
            Group group = match.Groups[groupCtr];
            outputBlock.Text += String.Format("   Group {0}: {1}", groupCtr, group.Value) + "\n";
            for (int captureCtr = 0; captureCtr < group.Captures.Count; captureCtr++)
               outputBlock.Text += String.Format("      Capture {0}: {1}", captureCtr,
                                 group.Captures[captureCtr].Value) + "\n";
         }
      }
   }
}
// The example displays the following output:
//       Match: Yes.
//          Group 0: Yes.
//             Capture 0: Yes.
//          Group 1: Yes.
//             Capture 0: Yes.
//          Group 2: Yes
//             Capture 0: Yes
//       Match: This dog is very friendly.
//          Group 0: This dog is very friendly.
//             Capture 0: This dog is very friendly.
//          Group 1: friendly.
//             Capture 0: This
//             Capture 1: dog
//             Capture 2: is
//             Capture 3: very
//             Capture 4: friendly.
//          Group 2: friendly
//             Capture 0: This
//             Capture 1: dog
//             Capture 2: is
//             Capture 3: very
//             Capture 4: friendly

The regular expression pattern ((\w+)[\s.])+ is defined as shown in the following table. Note that in this regular expression, a quantifier is applied to the entire regular expression.

Pattern

Description

(\w+)

Match one or more word characters. This is the second capturing group.

[\s.])

Match a white-space character or period (".").

((\w+)[\s.])

Match one or more word characters followed by a white-space character or period ("."). This is the first capturing group.

((\w+)[\s.])+

Match one or more occurrences of a word character or characters followed by a white-space character or period (".").

In this example, the input string consists of two sentences. As the output shows, because the first sentence consists of only one word, the CaptureCollection object has a single Capture object that represents the same capture as the Group object. Because the second sentence consists of multiple words, the Group objects only contain information about the last matched subexpression. Group 1, which represents the first capture, contains the last word in the sentence with a closing period. Group 2, which represents the second capture, contains the last word in the sentence. However, the Capture objects in the group's CaptureCollection object capture each subexpression match. The Capture objects in the first capturing group's collection of captures contain information about each captured word and white-space character or period. The Capture objects in the second capturing group's collection of captures contain information about each captured word.

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.

Change History

Date

History

Reason

August 2009

Expanded the Remarks section and replaced the example.

Customer feedback.