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
- 屬性
- 實作
範例
下列範例會比較 Group.Captures 和 Match.Captures
屬性所傳回 CaptureCollection 物件中的 Capture 物件。 它也會比較 Group 對象與 Group.Captures 屬性所傳回之 CaptureCollection 中的 Capture 物件。 這個範例會使用下列兩個正規表示式來尋找單一輸入字串中的相符專案:
\b\w+\W{1,2}
這個正則表達式模式會識別由一或多個單字字元組成的單字,後面接著一或兩個非文字字元,例如空格符或標點符號。 正則表達式不包含任何擷取群組。 此範例的輸出顯示 Match 物件和 CaptureCollection 物件 Group.Captures 和
Match.Captures
屬性包含相同相符專案的相關信息。(\b\w+\W{1,2})+
這個正則表達式模式會識別句子中的單字。 此模式會定義單一擷取群組,其中包含一或多個單字字元,後面接著一或兩個非單字字元。 正則表達式模式會使用
+
數量值來比對此群組的一或多個專案。 此範例的輸出顯示 Match 物件和Match.Captures
屬性所傳回的 CaptureCollection 物件包含相同相符專案的相關信息。 第二個 Group 對象,對應至正則表達式中唯一的擷取群組,只會識別最後一個擷取的字元串,而第一個擷取群組的 Group.Captures 屬性所傳回的 CaptureCollection 物件則包含所有擷取的子字串。
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 會針對每個擷取的子字串包含一個 Capture 物件,而 Group 物件只會提供最後一個擷取子字元串的相關信息。
Match.Captures
屬性。 在此情況下,集合是由單一 Capture 物件所組成,可提供整個相符專案的相關信息。 也就是說,CaptureCollection 物件會提供與 Match 物件相同的資訊。
若要逐一查看集合的成員,您應該使用語言所提供的集合反覆專案建構(例如 C# 中的 foreach
和 visual Basic 中的 For Each
...Next
...Next
),而不是擷取 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) |