Match 類別

定義

表示單一規則運算式 (Regular Expression) 比對的結果。

public ref class Match : System::Text::RegularExpressions::Group
public class Match : System.Text.RegularExpressions.Group
[System.Serializable]
public class Match : System.Text.RegularExpressions.Group
type Match = class
    inherit Group
[<System.Serializable>]
type Match = class
    inherit Group
Public Class Match
Inherits Group
繼承
屬性

範例

下列範例使用正則運算式 Console\.Write(Line)? 。 規則運算式解譯如下:

主控台 \ 。寫 比對字串 "Console.Write"。 請注意,會逸出 「.」 字元,使其解譯為常值句點,而不是符合任何字元的萬用字元。
(行) ? 比對出現零或一次的字串 "Line"。

範例 1

下列範例會呼叫 方法, Regex.Matches(String, String) 以擷取輸入字串中的所有模式相符專案。 然後逐一查看 MatchMatchCollection 回物件中的 物件,以顯示每個相符專案的相關資訊。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "int[] values = { 1, 2, 3 };\n" +
                     "for (int ctr = values.GetLowerBound(1); ctr <= values.GetUpperBound(1); ctr++)\n" +
                     "{\n" +
                     "   Console.Write(values[ctr]);\n" +
                     "   if (ctr < values.GetUpperBound(1))\n" +
                     "      Console.Write(\", \");\n" +
                     "}\n" +
                     "Console.WriteLine();\n";   
      
      string pattern = @"Console\.Write(Line)?";
      MatchCollection matches = Regex.Matches(input, pattern);
      foreach (Match match in matches)
         Console.WriteLine("'{0}' found in the source code at position {1}.",  
                           match.Value, match.Index);
   }
}
// The example displays the following output:
//    'Console.Write' found in the source code at position 112.
//    'Console.Write' found in the source code at position 184.
//    'Console.WriteLine' found in the source code at position 207.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "Dim values() As Integer = { 1, 2, 3 }" & vbCrLf & _
                            "For ctr As Integer = values.GetLowerBound(1) To values.GetUpperBound(1)" & vbCrLf & _
                            "   Console.Write(values(ctr))" & vbCrLf & _
                            "   If ctr < values.GetUpperBound(1) Then Console.Write("", "")" & vbCrLf & _
                            "Next" & vbCrLf & _
                            "Console.WriteLine()"   
      Dim pattern As String = "Console\.Write(Line)?"
      Dim matches As MatchCollection = Regex.Matches(input, pattern)
      For Each match As Match In matches
         Console.WriteLine("'{0}' found in the source code at position {1}.", _ 
                           match.Value, match.Index)       
      Next                            
   End Sub
End Module
' The example displays the following output:
'    'Console.Write' found in the source code at position 115.
'    'Console.Write' found in the source code at position 184.
'    'Console.WriteLine' found in the source code at position 211.

範例 2

下列範例會呼叫 Match(String, String)NextMatch 方法,一次擷取一個相符專案。

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "int[] values = { 1, 2, 3 };\n" +
                     "for (int ctr = values.GetLowerBound(1); ctr <= values.GetUpperBound(1); ctr++)\n" +
                     "{\n" +
                     "   Console.Write(values[ctr]);\n" +
                     "   if (ctr < values.GetUpperBound(1))\n" +
                     "      Console.Write(\", \");\n" +
                     "}\n" +
                     "Console.WriteLine();\n";   
      string pattern = @"Console\.Write(Line)?";
      Match match = Regex.Match(input, pattern);
      while (match.Success)
      {
         Console.WriteLine("'{0}' found in the source code at position {1}.",  
                           match.Value, match.Index);
         match = match.NextMatch();
      }
   }
}
// The example displays the following output:
//    'Console.Write' found in the source code at position 112.
//    'Console.Write' found in the source code at position 184.
//    'Console.WriteLine' found in the source code at position 207.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "Dim values() As Integer = { 1, 2, 3 }" & vbCrLf & _
                            "For ctr As Integer = values.GetLowerBound(1) To values.GetUpperBound(1)" & vbCrLf & _
                            "   Console.Write(values(ctr))" & vbCrLf & _
                            "   If ctr < values.GetUpperBound(1) Then Console.Write("", "")" & vbCrLf & _
                            "Next" & vbCrLf & _
                            "Console.WriteLine()"   
      Dim pattern As String = "Console\.Write(Line)?"
      Dim match As Match = Regex.Match(input, pattern)
      Do While match.Success
         Console.WriteLine("'{0}' found in the source code at position {1}.", _ 
                           match.Value, match.Index)
         match = match.NextMatch()                  
      Loop                            
   End Sub
End Module
' The example displays the following output:
'    'Console.Write' found in the source code at position 115.
'    'Console.Write' found in the source code at position 184.
'    'Console.WriteLine' found in the source code at position 211.

備註

物件 Match 是不可變的,而且沒有公用建構函式。 類別的 Match 實例是由 方法傳 Regex.Match 回,並代表字串中的第一個模式相符專案。 後續相符專案是由 Match 方法傳 Match.NextMatch 回的物件表示。 此外, MatchCollection 方法會傳回 Regex.Matches 包含零、一或多個 Match 物件的 物件。

Regex.Matches如果方法無法比對輸入字串中的正則運算式模式,它會傳回空 MatchCollection 的 物件。 然後,您可以在 C# 中使用 foreach 建構,或在 Visual Basic 中使用 建構來逐一 For Each 查看集合。

Regex.Match如果方法無法比對正則運算式模式,它會傳回 Match 等於 Match.Empty 的物件。 您可以使用 Success 屬性來判斷相符專案是否成功。 下列範例提供說明。

// Search for a pattern that is not found in the input string.
string pattern = "dog";
string input = "The cat saw the other cats playing in the back yard.";
Match match = Regex.Match(input, pattern);
if (match.Success )
   // Report position as a one-based integer.
   Console.WriteLine("'{0}' was found at position {1} in '{2}'.", 
                     match.Value, match.Index + 1, input);
else
   Console.WriteLine("The pattern '{0}' was not found in '{1}'.",
                     pattern, input);
' Search for a pattern that is not found in the input string.
Dim pattern As String = "dog"
Dim input As String = "The cat saw the other cats playing in the back yard."
Dim match As Match = Regex.Match(input, pattern)
If match.Success Then
   ' Report position as a one-based integer.
   Console.WriteLine("'{0}' was found at position {1} in '{2}'.", _ 
                     match.Value, match.Index + 1, input)
Else
   Console.WriteLine("The pattern '{0}' was not found in '{1}'.", _
                     pattern, input)
End If

如果模式比對成功, Value 屬性會包含相符的子字串、 Index 屬性會指出輸入字串中相符子字串之以零起始的起始位置,而 Length 屬性會指出輸入字串中相符子字串的長度。

因為單一比對可能會牽涉到多個擷取群組, Match 所以具有 Groups 傳回 的屬性 GroupCollectionMatch實例本身相當於集合中的第一個物件,在 Match.Groups[0] Visual Basic) 中的 (Match.Groups(0) ,代表整個相符專案。 您可以透過下列方式,以相符專案存取擷取的群組:

屬性

Captures

依照最內層最左邊為優先的順序,取得符合擷取群組的所有擷取的集合 (如果規則運算式使用 RightToLeft 選項修改,則依照最內層最右邊為優先的順序), 集合可能有零個或更多項目。

(繼承來源 Group)
Empty

取得空白群組。 所有失敗的比對會傳回這個空白比對。

Groups

取得符合規則運算式的群組集合。

Index

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

(繼承來源 Capture)
Length

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

(繼承來源 Capture)
Name

傳回目前執行個體所代表的擷取群組名稱。

(繼承來源 Group)
Success

取得值,指出比對是否成功。

(繼承來源 Group)
Value

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

(繼承來源 Capture)
ValueSpan

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

(繼承來源 Capture)

方法

Equals(Object)

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

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
NextMatch()

自最後一個比對結束的位置 (在最後符合字元之後的字元) 開始,傳回具有下一個比對結果的新 Match 物件。

Result(String)

傳回所指定取代模式的展開 (Expansion)。

Synchronized(Match)

傳回 Match 執行個體,相當於可在多個執行緒之間提供一個可共用的執行個體。

ToString()

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

(繼承來源 Capture)

適用於

另請參閱