CaptureCollection 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
代表單一擷取群組完成的擷取集合。 集合是不可變的 (唯讀),且沒有公用的建構函式。
public ref class CaptureCollection : System::Collections::ICollection
public ref class CaptureCollection : System::Collections::Generic::ICollection<System::Text::RegularExpressions::Capture ^>, System::Collections::Generic::IEnumerable<System::Text::RegularExpressions::Capture ^>, System::Collections::Generic::IList<System::Text::RegularExpressions::Capture ^>, System::Collections::Generic::IReadOnlyCollection<System::Text::RegularExpressions::Capture ^>, System::Collections::Generic::IReadOnlyList<System::Text::RegularExpressions::Capture ^>, System::Collections::IList
public class CaptureCollection : System.Collections.ICollection
public class CaptureCollection : System.Collections.Generic.ICollection<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IEnumerable<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IList<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IReadOnlyCollection<System.Text.RegularExpressions.Capture>, System.Collections.Generic.IReadOnlyList<System.Text.RegularExpressions.Capture>, System.Collections.IList
[System.Serializable]
public class CaptureCollection : System.Collections.ICollection
type CaptureCollection = class
interface ICollection
interface IEnumerable
type CaptureCollection = class
interface ICollection<Capture>
interface seq<Capture>
interface IEnumerable
interface IList<Capture>
interface IReadOnlyCollection<Capture>
interface IReadOnlyList<Capture>
interface ICollection
interface IList
type CaptureCollection = class
interface ICollection
interface IEnumerable
interface ICollection<Capture>
interface seq<Capture>
interface IList<Capture>
interface IReadOnlyCollection<Capture>
interface IReadOnlyList<Capture>
interface IList
[<System.Serializable>]
type CaptureCollection = class
interface ICollection
interface IEnumerable
Public Class CaptureCollection
Implements ICollection
Public Class CaptureCollection
Implements ICollection(Of Capture), IEnumerable(Of Capture), IList, IList(Of Capture), IReadOnlyCollection(Of Capture), IReadOnlyList(Of Capture)
- 繼承
-
CaptureCollection
- 屬性
- 實作
範例
下列範例會 Capture 比較 和 Match.Captures
屬性所 Group.Captures 傳回之 物件中的 CaptureCollection 物件。 它也會比較 Group 物件與 Capture 屬性所 Group.Captures 傳回之 中的 CaptureCollection 物件。 此範例會使用下列兩個正則運算式來尋找單一輸入字串中的相符專案:
\b\w+\W{1,2}
這個正則運算式模式會識別由一或多個文字字元組成的單字,後面接著一或兩個非文字字元,例如空白字元或標點符號。 正則運算式不包含任何擷取群組。 範例的輸出顯示 Match 和 屬性所 Group.Captures
Match.Captures
傳回的物件和 CaptureCollection 物件都包含相同相符專案的相關資訊。(\b\w+\W{1,2})+
這個正則運算式模式會識別句子中的單字。 模式會定義單一擷取群組,其中包含一或多個文字字元,後面接著一或兩個非單字字元。 正則運算式模式會
+
使用數量詞來比對此群組的一或多個專案。 此範例的輸出顯示 Match 屬性所Match.Captures
傳回的物件和 CaptureCollection 物件包含相同相符專案的相關資訊。 第二 Group 個對應至正則運算式中唯一擷取群組的物件只會識別最後一個擷取的字串,而 CaptureCollection 第一個擷取群組 Group.Captures 的 屬性所傳回的物件則包含所有擷取的子字串。
using System;
using System.Text.RegularExpressions;
public class Example
{
public static void Main()
{
string pattern;
string input = "The young, hairy, and tall dog slowly walked across the yard.";
Match 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 (int ctr = 0; ctr < match.Captures.Count; ctr++)
Console.WriteLine(" {0}: '{1}'", ctr, match.Captures[ctr].Value);
Console.WriteLine(" Match.Groups: {0}", match.Groups.Count);
for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
{
Console.WriteLine(" Group {0}: '{1}'",
groupCtr, match.Groups[groupCtr].Value);
Console.WriteLine(" Group({0}).Captures: {1}",
groupCtr, match.Groups[groupCtr].Captures.Count);
for (int captureCtr = 0; captureCtr < match.Groups[groupCtr].Captures.Count; captureCtr++)
Console.WriteLine(" Capture {0}: '{1}'",
captureCtr,
match.Groups[groupCtr].Captures[captureCtr].Value);
}
Console.WriteLine("-----\n");
// 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 (int ctr = 0; ctr < match.Captures.Count; ctr++)
Console.WriteLine(" {0}: '{1}'", ctr, match.Captures[ctr].Value);
Console.WriteLine(" Match.Groups: {0}", match.Groups.Count);
for (int groupCtr = 0; groupCtr < match.Groups.Count; groupCtr++)
{
Console.WriteLine(" Group {0}: '{1}'", groupCtr, match.Groups[groupCtr].Value);
Console.WriteLine(" Group({0}).Captures: {1}",
groupCtr, match.Groups[groupCtr].Captures.Count);
for (int captureCtr = 0; captureCtr < match.Groups[groupCtr].Captures.Count; captureCtr++)
Console.WriteLine(" Capture {0}: '{1}'", captureCtr, match.Groups[groupCtr].Captures[captureCtr].Value);
}
}
}
// 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.'
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.'
備註
集合是不可變的 (唯讀),且沒有公用的建構函式。 物件 CaptureCollection 包含一或多個 Capture 物件。
類別的 CaptureCollection 實例會由下列屬性傳回:
Group.Captures 屬性。 集合的每個成員都代表擷取群組所擷取的子字串。 如果數量詞未套用至擷取群組,則 CaptureCollection 包含單 Capture 一物件,代表與 Group 物件相同的擷取子字串。 如果數量詞套用至擷取群組,則會 CaptureCollection 針對每個擷取的子字串包含一個物件,而 Group 物件只會提供最後一個 Capture 擷取子字串的相關資訊。
Match.Captures
屬性。 在此情況下,集合是由單 Capture 一 物件所組成,提供整個相符專案的相關資訊。 也就是說, CaptureCollection 物件會提供與 Match 物件相同的資訊。
若要逐一查看集合的成員,您應該使用語言所提供的集合反復專案建構 (,例如 foreach
在 C# 和 For Each
Next
... 中 Visual Basic) ,而不是擷取 方法所傳回的 GetEnumerator 列舉值。
屬性
Count |
取得群組所擷取的子字串數目。 |
IsReadOnly |
取得值,這個值會指出集合是否唯讀。 |
IsSynchronized |
取得值,指示對集合的存取是否為同步的 (安全執行緒)。 |
Item[Int32] |
取得集合的個別成員。 |
SyncRoot |
取得物件,這個物件可以用來對集合進行同步存取。 |
方法
CopyTo(Array, Int32) |
複製集合的所有項目至指定索引處開始的指定陣列。 |
CopyTo(Capture[], Int32) | |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetEnumerator() |
提供逐一查看集合的列舉值。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |
明確介面實作
擴充方法
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應