Capture 類別

定義

代表單一子運算式成功擷取的結果。Represents the results from a single successful subexpression capture.

public ref class Capture
[System.Serializable]
public class Capture
type Capture = class
Public Class Capture
繼承
Capture
衍生
屬性

範例

下列範例定義的正則運算式會比對不含標點符號的句子,但句號(".")除外。The following example defines a regular expression that matches sentences that contain no punctuation except for a period (".").

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "Yes. This dog is very friendly.";
      string pattern = @"((\w+)[\s.])+";
      foreach (Match match in Regex.Matches(input, pattern))
      {
         Console.WriteLine("Match: {0}", match.Value);
         for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
         {
            Group group = match.Groups[groupCtr];
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value);
            for (int captureCtr = 0; captureCtr < group.Captures.Count; captureCtr++)
               Console.WriteLine("      Capture {0}: {1}", captureCtr, 
                                 group.Captures[captureCtr].Value);
         }                      
      }
   }
}
// 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
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      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)
         Console.WriteLine("Match: {0}", match.Value)
         For groupCtr As Integer = 0 To match.Groups.Count - 1
            Dim group As Group = match.Groups(groupCtr)
            Console.WriteLine("   Group {0}: {1}", groupCtr, group.Value)
            For captureCtr As Integer = 0 To group.Captures.Count - 1
               Console.WriteLine("      Capture {0}: {1}", captureCtr, _
                                 group.Captures(captureCtr).Value)
            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

規則運算式模式 ((\w+)[\s.])+ 的定義如下表所示。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. 如輸出所示,第一個句子只包含一個單字,因此 CaptureCollection 物件有一個代表與 Group 物件相同之捕捉的單一 Capture 物件。As the output shows, the first sentence consists of only one word, so the CaptureCollection object has a single Capture object that represents the same capture as the Group object. 第二個句子是由多個單字所組成,因此 Group 物件只會包含最後相符子運算式的相關資訊。The second sentence consists of multiple words, so the Group objects only contain information about the last matched subexpression. 代表第一個 capture 的群組1,包含句子中具有結束時間的最後一個單字。Group 1, which represents the first capture, contains the last word in the sentence that has a closing period. 群組2(代表第二個 capture)包含句子中的最後一個單字。Group 2, which represents the second capture, contains the last word in the sentence. 不過,群組 CaptureCollection 物件中的 Capture 物件會捕捉每個子運算式相符的。However, the Capture objects in the group's CaptureCollection object capture each subexpression match. 第一個捕獲群組的「捕獲」集合中的 Capture 物件,會包含每個已捕獲的單字和空白字元或句號的相關資訊。The Capture objects in the first capturing group's collection of captures contain information about each captured word and white-space character or period. 第二個捕獲群組的「捕獲」集合中的 Capture 物件包含每個已捕獲單字的相關資訊。The Capture objects in the second capturing group's collection of captures contain information about each captured word.

備註

Capture 物件是不可變的,而且沒有公用的函式。A Capture object is immutable and has no public constructor. 實例會透過 CaptureCollection 物件傳回,而此物件是由 Match.CapturesGroup.Captures 屬性傳回。Instances are returned through the CaptureCollection object, which is returned by the Match.Captures and Group.Captures properties. 不過,Match.Captures 屬性會提供與 Match 物件相同之相符項的相關資訊。However, the Match.Captures property provides information about the same match as the Match object.

如果您未將數量詞套用至捕捉群組,則 Group.Captures 屬性會傳回具有單一 Capture 物件的 CaptureCollection,其中會提供與 Group 物件相同之捕捉的相關資訊。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. 如果您將數量詞套用至捕捉群組,[Group.Index]、[Group.Length] 和 [Group.Value 屬性] 只會提供最後一個已捕獲群組的相關資訊,而 CaptureCollection 中的 Capture 物件則會提供所有子運算式捕捉的相關資訊。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.

屬性

Index

在原始字串中找到擷取的子字串的第一個字元之位置。The position in the original string where the first character of the captured substring is found.

Length

取得所擷取子字串的長度。Gets the length of the captured substring.

Value

從輸入字串取得擷取的子字串。Gets the captured substring from the input string.

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(繼承來源 Object)
GetHashCode()

作為預設雜湊函數。Serves as the default hash function.

(繼承來源 Object)
GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(繼承來源 Object)
ToString()

擷取已透過呼叫 Value 屬性從輸入字串擷取的子字串。Retrieves the captured substring from the input string by calling the Value property.

適用於

另請參閱