MatchCollection MatchCollection MatchCollection MatchCollection Class

定義

表示藉由重複套用規則運算式 (Regular Expression) 模式至輸入字串,所找到的成功比對的集合。Represents the set of successful matches found by iteratively applying a regular expression pattern to the input string.

public ref class MatchCollection : System::Collections::ICollection
[System.Serializable]
public class MatchCollection : System.Collections.ICollection
type MatchCollection = class
    interface ICollection
    interface IEnumerable
Public Class MatchCollection
Implements ICollection
繼承
MatchCollectionMatchCollectionMatchCollectionMatchCollection
屬性
實作

範例

下列範例說明如何使用MatchCollection類別來詢問一Match組實例。The following example illustrates the use of the MatchCollection class to interrogate a set of Match instances.

#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;
int main()
{
   // Define a regular expression for repeated words.
   Regex^ rx = gcnew Regex( "\\b(?<word>\\w+)\\s+(\\k<word>)\\b",static_cast<RegexOptions>(RegexOptions::Compiled | RegexOptions::IgnoreCase) );

   // Define a test string.        
   String^ text = "The the quick brown fox  fox jumps over the lazy dog dog.";

   // Find matches.
   MatchCollection^ matches = rx->Matches( text );

   // Report the number of matches found.
   Console::WriteLine( "{0} matches found.", matches->Count );

   // Report on each match.
   for each (Match^ match in matches)
   {
      String^ word = match->Groups["word"]->Value;
      int index = match->Index;
      Console::WriteLine("{0} repeated at position {1}", word, index);   
   }
}
using System;
using System.Text.RegularExpressions;

public class Test
{

    public static void Main ()
    {

        // Define a regular expression for repeated words.
        Regex rx = new Regex(@"\b(?<word>\w+)\s+(\k<word>)\b",
          RegexOptions.Compiled | RegexOptions.IgnoreCase);

        // Define a test string.        
        string text = "The the quick brown fox  fox jumps over the lazy dog dog.";
        
        // Find matches.
        MatchCollection matches = rx.Matches(text);

        // Report the number of matches found.
        Console.WriteLine("{0} matches found in:\n   {1}", 
                          matches.Count, 
                          text);

        // Report on each match.
        foreach (Match match in matches)
        {
            GroupCollection groups = match.Groups;
            Console.WriteLine("'{0}' repeated at positions {1} and {2}",  
                              groups["word"].Value, 
                              groups[0].Index, 
                              groups[1].Index);
        }
        
    }
    
}
// The example produces the following output to the console:
//       3 matches found in:
//          The the quick brown fox  fox jumps over the lazy dog dog.
//       'The' repeated at positions 0 and 4
//       'fox' repeated at positions 20 and 25
//       'dog' repeated at positions 50 and 54
Imports System.Text.RegularExpressions

Public Module Test

    Public Sub Main()
        ' Define a regular expression for repeated words.
        Dim rx As New Regex("\b(?<word>\w+)\s+(\k<word>)\b", _
               RegexOptions.Compiled Or RegexOptions.IgnoreCase)

        ' Define a test string.        
        Dim text As String = "The the quick brown fox  fox jumps over the lazy dog dog."
        
        ' Find matches.
        Dim matches As MatchCollection = rx.Matches(text)

        ' Report the number of matches found.
        Console.WriteLine("{0} matches found in:", matches.Count)
        Console.WriteLine("   {0}", text)

        ' Report on each match.
        For Each match As Match In matches
            Dim groups As GroupCollection = match.Groups
            Console.WriteLine("'{0}' repeated at positions {1} and {2}", _ 
                              groups.Item("word").Value, _
                              groups.Item(0).Index, _
                              groups.Item(1).Index)
        Next
    End Sub
End Module
' The example produces the following output to the console:
'       3 matches found in:
'          The the quick brown fox  fox jumps over the lazy dog dog.
'       'The' repeated at positions 0 and 4
'       'fox' repeated at positions 20 and 25
'       'dog' repeated at positions 50 and 54

備註

集合是不可變的 (唯讀), 而且沒有公用的函式。The collection is immutable (read-only) and has no public constructor. Regex.Matches 方法會傳回 MatchCollection 物件。The Regex.Matches method returns a MatchCollection object.

集合包含零個或多System.Text.RegularExpressions.Match個物件。The collection contains zero or more System.Text.RegularExpressions.Match objects. 如果比對成功, 則會針對輸入字串中找到System.Text.RegularExpressions.Match的每個相符項, 將集合填入一個物件。If the match is successful, the collection is populated with one System.Text.RegularExpressions.Match object for each match found in the input string. 如果比對不成功, 則集合不會System.Text.RegularExpressions.Match包含任何物件, Count且其屬性等於零。If the match is unsuccessful, the collection contains no System.Text.RegularExpressions.Match objects, and its Count property equals zero.

將正則運算式模式套用到特定的輸入字串時, 正則運算式引擎會使用兩種技術之一來MatchCollection建立物件:When applying a regular expression pattern to a particular input string, the regular expression engine uses either of two techniques to build the MatchCollection object:

  • 直接評估。Direct evaluation.

    物件會一次填入全部, 其中包含對方法的特定呼叫所Regex.Matches產生的所有相符專案。 MatchCollectionThe MatchCollection object is populated all at once, with all matches resulting from a particular call to the Regex.Matches method. 存取集合的Count屬性時, 會使用這項技術。This technique is used when the collection's Count property is accessed. 這通常是擴展集合的較昂貴方法, 而且需要更高的效能。It typically is the more expensive method of populating the collection and entails a greater performance hit.

  • 延遲評估。Lazy evaluation.

    會視需要在比對時填入物件。MatchCollectionThe MatchCollection object is populated as needed on a match-by-match basis. 它相當於正則運算式引擎會重複呼叫Regex.Match方法, 並將每個相符項新增至集合。It is equivalent to the regular expression engine calling the Regex.Match method repeatedly and adding each match to the collection. 這項技術是GetEnumerator在透過其方法存取集合時使用, 或在foreach使用語句C# For Each(在中) 或 .。。Next語句 (在 Visual Basic 中)。This technique is used when the collection is accessed through its GetEnumerator method, or when it is accessed using the foreach statement (in C#) or the For Each...Next statement (in Visual Basic).

若要逐一查看集合的成員, 您應該使用您的語言所提供的集合反復專案結構 (例如foreach在C#和For Each中 ...NextTo iterate through the members of the collection, you should use the collection iteration construct provided by your language (such as foreach in C# and For EachNext 在 Visual Basic 中), 而不是抓取GetEnumerator方法所傳回的列舉值。in Visual Basic) instead of retrieving the enumerator that is returned by the GetEnumerator method.

屬性

Count Count Count Count

取得符合項的數目。Gets the number of matches.

IsReadOnly IsReadOnly IsReadOnly IsReadOnly

取得值,這個值會指出集合是否唯讀。Gets a value that indicates whether the collection is read only.

IsSynchronized IsSynchronized IsSynchronized IsSynchronized

取得值,指出對集合的存取是否為同步的 (安全執行緒)。Gets a value indicating whether access to the collection is synchronized (thread-safe).

Item[Int32] Item[Int32] Item[Int32] Item[Int32]

取得集合的個別成員。Gets an individual member of the collection.

SyncRoot SyncRoot SyncRoot SyncRoot

取得物件,這個物件可以用來對集合進行同步存取。Gets an object that can be used to synchronize access to the collection.

方法

CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32) CopyTo(Array, Int32)

複製集合的所有元素到指定索引處開始的指定陣列。Copies all the elements of the collection to the given array starting at the given index.

CopyTo(Match[], Int32) CopyTo(Match[], Int32) CopyTo(Match[], Int32) CopyTo(Match[], Int32)
Equals(Object) Equals(Object) Equals(Object) Equals(Object)

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

(Inherited from Object)
GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

提供逐一查看集合的列舉值。Provides an enumerator that iterates through the collection.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

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

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32) ICollection.CopyTo(Array, Int32)

將集合的所有元素複製到以指定的索引為起始點的指定陣列。Copies all the elements of the collection to the specified array starting at the specified index.

ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized ICollection.IsSynchronized

取得值,指示對集合的存取是否為同步的 (安全執行緒)。Gets a value that indicates whether access to the collection is synchronized (thread-safe).

ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot ICollection.SyncRoot

取得物件,這個物件可以用來對集合進行同步存取。Gets an object that can be used to synchronize access to the collection.

ICollection<Match>.Add(Match) ICollection<Match>.Add(Match) ICollection<Match>.Add(Match) ICollection<Match>.Add(Match)
ICollection<Match>.Clear() ICollection<Match>.Clear() ICollection<Match>.Clear() ICollection<Match>.Clear()
ICollection<Match>.Contains(Match) ICollection<Match>.Contains(Match) ICollection<Match>.Contains(Match) ICollection<Match>.Contains(Match)
ICollection<Match>.Remove(Match) ICollection<Match>.Remove(Match) ICollection<Match>.Remove(Match) ICollection<Match>.Remove(Match)
IEnumerable<Match>.GetEnumerator() IEnumerable<Match>.GetEnumerator() IEnumerable<Match>.GetEnumerator() IEnumerable<Match>.GetEnumerator()
IList.Add(Object) IList.Add(Object) IList.Add(Object) IList.Add(Object)
IList.Clear() IList.Clear() IList.Clear() IList.Clear()
IList.Contains(Object) IList.Contains(Object) IList.Contains(Object) IList.Contains(Object)
IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object) IList.IndexOf(Object)
IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object) IList.Insert(Int32, Object)
IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize IList.IsFixedSize
IList.Item[Int32] IList.Item[Int32] IList.Item[Int32] IList.Item[Int32]
IList.Remove(Object) IList.Remove(Object) IList.Remove(Object) IList.Remove(Object)
IList.RemoveAt(Int32) IList.RemoveAt(Int32) IList.RemoveAt(Int32) IList.RemoveAt(Int32)
IList<Match>.IndexOf(Match) IList<Match>.IndexOf(Match) IList<Match>.IndexOf(Match) IList<Match>.IndexOf(Match)
IList<Match>.Insert(Int32, Match) IList<Match>.Insert(Int32, Match) IList<Match>.Insert(Int32, Match) IList<Match>.Insert(Int32, Match)
IList<Match>.Item[Int32] IList<Match>.Item[Int32] IList<Match>.Item[Int32] IList<Match>.Item[Int32]
IList<Match>.RemoveAt(Int32) IList<Match>.RemoveAt(Int32) IList<Match>.RemoveAt(Int32) IList<Match>.RemoveAt(Int32)

擴充方法

Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable) Cast<TResult>(IEnumerable)

IEnumerable 的項目轉換成指定的型別。Casts the elements of an IEnumerable to the specified type.

OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable) OfType<TResult>(IEnumerable)

根據指定的型別來篩選 IEnumerable 的項目。Filters the elements of an IEnumerable based on a specified type.

AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable) AsParallel(IEnumerable)

啟用查詢的平行化作業。Enables parallelization of a query.

AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable) AsQueryable(IEnumerable)

IEnumerable 轉換成 IQueryableConverts an IEnumerable to an IQueryable.

適用於