Capture 類別

定義

代表單一子運算式成功擷取的結果。

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

範例

下列範例會定義正則運算式,其符合不含標點符號的句子,但句點 (除外」。) 。

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.])+ 的定義如下表所示。 請注意,在此正則運算式中,會將數量詞 (+) 套用至整個正則運算式。

模式 描述
(\w+) 比對一個或多個文字字元。 這是第二個擷取群組。
[\s.]) 比對空白字元或句號 (「。」) 。
((\w+)[\s.]) 比對一或多個文字字元,後面接著空白字元或句號 (「。」) 。 這是第一個擷取群組。
((\w+)[\s.])+ 比對一或多個出現的單字字元或字元,後面接著空白字元或句號 (「。」) 。

在此範例中,輸入字串是由兩個句子所組成。 如輸出所示,第一個句子只包含一個單字,因此 CaptureCollection 物件具有單 Capture 一物件,代表與 Group 物件相同的擷取。 第二個句子是由多個單字所組成,因此 Group 物件只會包含最後一個相符子運算式的相關資訊。 代表第一個擷取的群組 1 包含結尾句點的最後一個單字。 代表第二個擷取的群組 2 包含句子中的最後一個字。 不過,群組 CaptureCaptureCollection 物件中的物件會擷取每個子運算式相符專案。 第 Capture 一個擷取群組擷取集合中的物件包含每個擷取的文字和空白字元或句號的相關資訊。 第 Capture 二個擷取群組擷取集合中的物件包含每個擷取字組的相關資訊。

備註

Capture物件是不可變的,而且沒有公用建構函式。 實例會透過 CaptureCollection 物件傳回,由 和 Group.Captures 屬性傳 Match.Captures 回。 不過, Match.Captures 屬性會提供與 物件相同相符 Match 專案的相關資訊。

如果您未將數量詞套用至擷取群組,屬性會 Group.Captures 傳回 CaptureCollection 具有單 Capture 一 物件,以提供與物件相同擷取 Group 的相關資訊。 如果您將數量詞套用至擷取群組, Group.IndexGroup.LengthGroup.Value 屬性只會提供最後一個擷取群組的相關資訊,而 中的 CaptureCollection 物件則 Capture 提供所有子運算式擷取的相關資訊。 這個範例將提供說明。

屬性

Index

在原始字串中找到擷取的子字串的第一個字元之位置。

Length

取得所擷取子字串的長度。

Value

從輸入字串取得擷取的子字串。

ValueSpan

從輸入字串取得擷取範圍。

方法

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

擷取已透過呼叫 Value 屬性從輸入字串擷取的子字串。

適用於

另請參閱