Match 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
表示单个正则表达式匹配的结果。
public ref class Match : System::Text::RegularExpressions::Grouppublic class Match : System.Text.RegularExpressions.Group[System.Serializable]
public class Match : System.Text.RegularExpressions.Grouptype Match = class
    inherit Group[<System.Serializable>]
type Match = class
    inherit GroupPublic Class Match
Inherits Group- 继承
- 属性
示例
以下示例使用正则表达式 Console\.Write(Line)?。 正则表达式按如下方式解释:
| 控制台\。写 | 匹配字符串“Console.Write”。 请注意,将转义“.”字符,以便将其解释为文本句点,而不是与任何字符匹配的通配符。 | 
| (行) ? | 匹配字符串“Line”的零个或一个匹配项。 | 
示例 1
以下示例调用 Regex.Matches(String, String) 该方法以检索输入字符串中的所有模式匹配项。 然后循环访问 Match 返回 MatchCollection 的对象,以显示有关每个匹配项的信息。
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 是不可变的,没有公共构造函数。 方法返回Regex.Match类的Match实例,并表示字符串中的第一个模式匹配。 后续匹配由 Match 方法返回 Match.NextMatch 的对象表示。 此外,MatchCollection方法返回Regex.Matches由零、一个或多个Match对象组成的对象。
Regex.Matches如果该方法无法匹配输入字符串中的正则表达式模式,则返回一个空MatchCollection对象。 然后foreach,可以使用 C# 中的构造或For EachVisual Basic中的构造来循环访问集合。
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 该 GroupCollection属性的属性。 实例Match本身等效于集合中的第一个对象,Visual Basic) Match.Groups[0] 中的 (Match.Groups(0),表示整个匹配项。 可以通过以下方式访问捕获的组:
- 可以使用 (C#) 或 - For Each(Visual Basic) 构造循环访问对象- foreach的成员GroupCollection。
- 可以使用该 GroupCollection.Item[Int32] 属性按捕获组的数目来检索组。 请注意,可以通过调用实例 Regex.GetGroupNumbers 方法来确定正则表达式中存在哪些编号组。 
- 可以使用该 GroupCollection.Item[String] 属性按捕获组的名称检索组。 请注意,可以通过调用实例 Regex.GetGroupNames() 方法来确定正则表达式中存在哪些命名组。 
属性
| 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) | 返回对指定替换模式的扩展。 | 
| Synchronized(Match) | 返回一个与提供的实例等效的 Match 实例,该实例适合在多个线程间共享。 | 
| ToString() | 通过调用 Value 属性,从输入的字符串中检索捕获的子字符串。(继承自 Capture) |