Regex.Matches 方法 
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在输入字符串中搜索正则表达式的所有匹配项并返回所有匹配。
重载
| Matches(String, String, RegexOptions, TimeSpan) | 使用指定的匹配选项和超时间隔在指定的输入字符串中搜索指定的正则表达式的所有匹配项。 | 
| Matches(String, String, RegexOptions) | 使用指定的匹配选项在指定的输入字符串中搜索指定的正则表达式的所有匹配项。 | 
| Matches(String, Int32) | 从字符串中的指定起始位置开始,在指定的输入字符串中搜索正则表达式的所有匹配项。 | 
| Matches(String) | 在指定的输入字符串中搜索正则表达式的所有匹配项。 | 
| Matches(String, String) | 在指定的输入字符串中搜索指定的正则表达式的所有匹配项。 | 
Matches(String, String, RegexOptions, TimeSpan)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
使用指定的匹配选项和超时间隔在指定的输入字符串中搜索指定的正则表达式的所有匹配项。
public:
 static System::Text::RegularExpressions::MatchCollection ^ Matches(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options, TimeSpan matchTimeout);public static System.Text.RegularExpressions.MatchCollection Matches (string input, string pattern, System.Text.RegularExpressions.RegexOptions options, TimeSpan matchTimeout);static member Matches : string * string * System.Text.RegularExpressions.RegexOptions * TimeSpan -> System.Text.RegularExpressions.MatchCollectionPublic Shared Function Matches (input As String, pattern As String, options As RegexOptions, matchTimeout As TimeSpan) As MatchCollection参数
- input
- String
要搜索匹配项的字符串。
- pattern
- String
要匹配的正则表达式模式。
- options
- RegexOptions
枚举值的按位组合,这些枚举值指定用于匹配的选项。
- matchTimeout
- TimeSpan
超时间隔;若要指示该方法不应超时,则为 InfiniteMatchTimeout。
返回
搜索操作找到的 Match 对象的集合。 如果未找到匹配项,则此方法将返回一个空集合对象。
例外
出现正则表达式分析错误。
              input 或 pattern 为 null。
示例
以下示例调用 Matches(String, String, RegexOptions, TimeSpan) 方法以执行区分大小写的比较,该比较匹配以“es”结尾的句子中的任何单词。 然后, Matches(String, String, RegexOptions, TimeSpan) 它调用 方法对模式与输入字符串执行不区分大小写的比较。 在这两种情况下,超时间隔都设置为 1 秒。 如输出所示,这两种方法返回不同的结果。
using System;
using System.Text.RegularExpressions;
public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      string sentence = "NOTES: Any notes or comments are optional.";
      
      // Call Matches method without specifying any options.
      try {
         foreach (Match match in Regex.Matches(sentence, pattern,
                                               RegexOptions.None,
                                               TimeSpan.FromSeconds(1)))
            Console.WriteLine("Found '{0}' at position {1}", 
                              match.Value, match.Index);
      }
      catch (RegexMatchTimeoutException) {
         // Do Nothing: Assume that timeout represents no match.
      }
      Console.WriteLine();
      // Call Matches method for case-insensitive matching.
      try { 
         foreach (Match match in Regex.Matches(sentence, pattern, RegexOptions.IgnoreCase))
            Console.WriteLine("Found '{0}' at position {1}", 
                              match.Value, match.Index);
      }
      catch (RegexMatchTimeoutException) {}
   }
}
// The example displays the following output:
//       Found 'notes' at position 11
//       
//       Found 'NOTES' at position 0
//       Found 'notes' at position 11
Imports System.Text.RegularExpressions
Module Example
   Public Sub Main()
      Dim pattern As String = "\b\w+es\b"
      Dim sentence As String = "NOTES: Any notes or comments are optional."
      
      ' Call Matches method without specifying any options.
      For Each match As Match In Regex.Matches(sentence, pattern, 
                                               RegexOptions.None, 
                                               TimeSpan.FromSeconds(1))
         Try
            Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index)
         Catch e As RegexMatchTimeoutException
            ' Do Nothing: Assume that timeout represents no match.
         End Try
      Next
      Console.WriteLine()
      
      ' Call Matches method for case-insensitive matching.
      Try
         For Each match As Match In Regex.Matches(sentence, pattern, 
                                                  RegexOptions.IgnoreCase,
                                                  TimeSpan.FromSeconds(1))
            Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index)
         Next
      Catch de As RegexMatchTimeoutException
         ' Do Nothing: Assume that timeout represents no match.
      End Try
   End Sub
End Module
' The example displays the following output:
'       Found 'notes' at position 11
'       
'       Found 'NOTES' at position 0
'       Found 'notes' at position 11
正则表达式模式 \b\w+es\b 的定义如下表所示。
| 模式 | 描述 | 
|---|---|
| \b | 在单词边界处开始匹配。 | 
| \w+ | 匹配一个或多个单词字符。 | 
| es | 匹配文本字符串“es”。 | 
| \b | 在单词边界处结束匹配。 | 
注解
方法 Matches(String, String, RegexOptions, TimeSpan) 类似于 Match(String, String, RegexOptions, TimeSpan) 方法,只不过它返回有关在输入字符串中找到的所有匹配项的信息,而不是单个匹配项。 它等效于以下代码:
   try {
      Match match = Regex.Match(input, pattern, options,
                                TimeSpan.FromSeconds(1));
      while (match.Success) {
            // Handle match here...
            match = match.NextMatch();
      }  
   }
   catch (RegexMatchTimeoutException) {
      // Do nothing: assume that exception represents no match.
   }
   Try
      Dim match As Match = Regex.Match(input, pattern, options, 
                                       TimeSpan.FromSeconds(1))
      Do While match.Success
            ' Handle match here...
            match = match.NextMatch()
      Loop  
   Catch e As RegexMatchTimeoutException
      ' Do nothing: assume that exception represents no match.
   End Try
静态 Matches 方法等效于使用指定的正则表达式模式构造 Regex 对象并调用实例方法 Matches。
参数 pattern 由正则表达式语言元素组成,这些元素以符号方式描述要匹配的字符串。 有关正则表达式的详细信息,请参阅 .NET 正则表达式 和 正则表达式语言 - 快速参考。
方法 Matches 使用延迟计算来填充返回 MatchCollection 的对象。 访问此集合的成员(如 MatchCollection.Count 和 MatchCollection.CopyTo )会导致立即填充集合。 若要利用延迟计算,应使用 C# 和 For Each...Next等foreach构造在 Visual Basic 中循环访问集合。
由于计算延迟,调用 Matches 方法不会引发 RegexMatchTimeoutException 异常。 但是,如果匹配的操作超过了 参数指定的matchTimeout超时间隔,则在对此方法返回的对象执行MatchCollection操作时,将引发异常。
调用方说明
建议将 参数设置为 matchTimeout 适当的值,例如 2 秒。 如果通过指定 InfiniteMatchTimeout来禁用超时,则正则表达式引擎提供的性能稍好一些。 但是,应仅在以下情况下禁用超时:
- 当正则表达式处理的输入派生自已知且受信任的源或由静态文本组成时。 这不包括用户动态输入的文本。 
- 当正则表达式模式经过全面测试以确保它有效地处理匹配项、非匹配项和接近匹配项时。 
- 当正则表达式模式不包含已知在处理接近匹配时导致过度回溯的语言元素时。 
另请参阅
适用于
Matches(String, String, RegexOptions)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
使用指定的匹配选项在指定的输入字符串中搜索指定的正则表达式的所有匹配项。
public:
 static System::Text::RegularExpressions::MatchCollection ^ Matches(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options);public static System.Text.RegularExpressions.MatchCollection Matches (string input, string pattern, System.Text.RegularExpressions.RegexOptions options);static member Matches : string * string * System.Text.RegularExpressions.RegexOptions -> System.Text.RegularExpressions.MatchCollectionPublic Shared Function Matches (input As String, pattern As String, options As RegexOptions) As MatchCollection参数
- input
- String
要搜索匹配项的字符串。
- pattern
- String
要匹配的正则表达式模式。
- options
- RegexOptions
枚举值的按位组合,这些枚举值指定用于匹配的选项。
返回
搜索操作找到的 Match 对象的集合。 如果未找到匹配项,则此方法将返回一个空集合对象。
例外
出现正则表达式分析错误。
              input 或 pattern 为 null。
              options 不是 RegexOptions 值的有效按位组合。
示例
以下示例调用 Matches(String, String) 方法以标识以“es”结尾的句子中的任何单词,然后调用 Matches(String, String, RegexOptions) 方法对模式与输入字符串执行不区分大小写的比较。 如输出所示,这两种方法返回不同的结果。
using System;
using System.Text.RegularExpressions;
public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      string sentence = "NOTES: Any notes or comments are optional.";
      
      // Call Matches method without specifying any options.
      foreach (Match match in Regex.Matches(sentence, pattern))
         Console.WriteLine("Found '{0}' at position {1}", 
                           match.Value, match.Index);
      Console.WriteLine();
      // Call Matches method for case-insensitive matching.
      foreach (Match match in Regex.Matches(sentence, pattern, RegexOptions.IgnoreCase))
         Console.WriteLine("Found '{0}' at position {1}", 
                           match.Value, match.Index);
   }
}
// The example displays the following output:
//       Found 'notes' at position 11
//       
//       Found 'NOTES' at position 0
//       Found 'notes' at position 11
Imports System.Text.RegularExpressions
Module Example
   Public Sub Main()
      Dim pattern As String = "\b\w+es\b"
      Dim sentence As String = "NOTES: Any notes or comments are optional."
      
      ' Call Matches method without specifying any options.
      For Each match As Match In Regex.Matches(sentence, pattern)
         Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index)
      Next
      Console.WriteLine()
      
      ' Call Matches method for case-insensitive matching.
      For Each match As Match In Regex.Matches(sentence, pattern, RegexOptions.IgnoreCase)
         Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index)
      Next
   End Sub
End Module
' The example displays the following output:
'       Found 'notes' at position 11
'       
'       Found 'NOTES' at position 0
'       Found 'notes' at position 11
正则表达式模式 \b\w+es\b 的定义如下表所示。
| 模式 | 描述 | 
|---|---|
| \b | 在单词边界处开始匹配。 | 
| \w+ | 匹配一个或多个单词字符。 | 
| es | 匹配文本字符串“es”。 | 
| \b | 在单词边界处结束匹配。 | 
注解
方法 Matches(String, String, RegexOptions) 类似于 Match(String, String, RegexOptions) 方法,只不过它返回有关在输入字符串中找到的所有匹配项的信息,而不是单个匹配项。 它等效于以下代码:
Match match = Regex.Match(input, pattern, options);
while (match.Success) {
      // Handle match here...
      match = match.NextMatch();
}
Dim match As Match = Regex.Match(input, pattern, options)
Do While match.Success
      ' Handle match here...
      match = match.NextMatch()
Loop
静态 Matches 方法等效于使用指定的正则表达式模式构造 Regex 对象并调用实例方法 Matches。
参数 pattern 由正则表达式语言元素组成,这些元素以符号方式描述要匹配的字符串。 有关正则表达式的详细信息,请参阅 .NET 正则表达式 和 正则表达式语言 - 快速参考。
方法 Matches 使用延迟计算来填充返回 MatchCollection 的对象。 访问此集合的成员(如 MatchCollection.Count 和 MatchCollection.CopyTo )会导致立即填充集合。 若要利用延迟计算,应使用 C# 和 For Each...Next等foreach构造在 Visual Basic 中循环访问集合。
由于计算延迟,调用 Matches(String, String) 方法不会引发 RegexMatchTimeoutException 异常。 但是,当对 MatchCollection 此方法返回的对象执行操作时,如果超时间隔由当前应用程序域的“REGEX_DEFAULT_MATCH_TIMEOUT”属性定义,并且匹配的操作超过了此超时间隔,则会引发异常。
调用方说明
此方法在一个间隔后超时,该间隔等于调用它的应用程序域的默认超时值。 如果尚未为应用程序域定义超时值,则使用值 InfiniteMatchTimeout(用于防止方法超时)。 用于检索多个模式匹配项 Matches(String, String, RegexOptions, TimeSpan)的建议静态方法是 ,它允许设置超时间隔。
另请参阅
适用于
Matches(String, Int32)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
从字符串中的指定起始位置开始,在指定的输入字符串中搜索正则表达式的所有匹配项。
public:
 System::Text::RegularExpressions::MatchCollection ^ Matches(System::String ^ input, int startat);public System.Text.RegularExpressions.MatchCollection Matches (string input, int startat);member this.Matches : string * int -> System.Text.RegularExpressions.MatchCollectionPublic Function Matches (input As String, startat As Integer) As MatchCollection参数
- input
- String
要搜索匹配项的字符串。
- startat
- Int32
在输入字符串中开始搜索的字符位置。
返回
搜索操作找到的 Match 对象的集合。 如果未找到匹配项,则此方法将返回一个空集合对象。
例外
              input 为 null。
              startat 小于零或大于 input 的长度。
示例
以下示例使用 Match(String) 方法查找以“es”结尾的句子中的第一个单词,然后调用 Matches(String, Int32) 方法以标识以“es”结尾的任何其他单词。
using System;
using System.Text.RegularExpressions;
public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      Regex rgx = new Regex(pattern);
      string sentence = "Who writes these notes and uses our paper?";
      
      // Get the first match.
      Match match = rgx.Match(sentence);
      if (match.Success) {
         Console.WriteLine("Found first 'es' in '{0}' at position {1}", 
                           match.Value, match.Index);
         // Get any additional matches.
         foreach (Match m in rgx.Matches(sentence, match.Index + match.Length))
            Console.WriteLine("Also found '{0}' at position {1}", 
                              m.Value, m.Index);
      }   
   }
}
// The example displays the following output:
//       Found first 'es' in 'writes' at position 4
//       Also found 'notes' at position 17
//       Also found 'uses' at position 27
Imports System.Text.RegularExpressions
Module Example
   Public Sub Main()
      Dim pattern As String = "\b\w+es\b"
      Dim rgx As New Regex(pattern)
      Dim sentence As String = "Who writes these notes and uses our paper?"
      
      ' Get the first match.
      Dim match As Match = rgx.Match(sentence)
      If match.Success Then
         Console.WriteLine("Found first 'es' in '{0}' at position {1}", _
                           match.Value, match.Index)
         ' Get any additional matches.
         For Each match In rgx.Matches(sentence, match.Index + match.Length)
            Console.WriteLine("Also found '{0}' at position {1}", _
                              match.Value, match.Index)
         Next
      End If   
   End Sub
End Module
' The example displays the following output:
'       Found first 'es' in 'writes' at position 4
'       Also found 'notes' at position 17
'       Also found 'uses' at position 27
正则表达式模式 \b\w+es\b 的定义如下表所示。
| 模式 | 描述 | 
|---|---|
| \b | 在单词边界处开始匹配。 | 
| \w+ | 匹配一个或多个单词字符。 | 
| es | 匹配文本字符串“es”。 | 
| \b | 在单词边界处结束匹配。 | 
注解
方法 Matches(String, Int32) 类似于 Match(String, Int32) 方法,只不过它返回有关在输入字符串中找到的所有匹配项的信息,而不是单个匹配项。 它等效于以下代码:
Match match = regex.Match(input, startAt);
while (match.Success) {
      // Handle match here...
      match = match.NextMatch();
}
Dim match As Match = regex.Match(input, startAt)
Do While match.Success
      ' Handle match here...
      match = match.NextMatch()
Loop
方法搜索的 Matches(String, Int32) 正则表达式模式是通过调用其中一个 Regex 类构造函数定义的。 有关可形成正则表达式模式的元素的详细信息,请参阅 正则表达式语言 - 快速参考。
有关 的 startat更多详细信息,请参阅 的 Match(String, Int32)“备注”部分。
方法 Matches 使用延迟计算来填充返回 MatchCollection 的对象。 访问此集合的成员(如 MatchCollection.Count 和 MatchCollection.CopyTo )会导致立即填充集合。 若要利用延迟计算,应使用 C# 和 For Each...Next等foreach构造在 Visual Basic 中循环访问集合。
由于计算延迟,调用 Matches(String, Int32) 方法不会引发 RegexMatchTimeoutException 异常。 但是,当对 MatchCollection 此方法返回的对象执行操作时,如果 MatchTimeout 属性不 Regex.InfiniteMatchTimeout 为 ,并且匹配的操作超过超时间隔,则会引发异常。
另请参阅
适用于
Matches(String)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
在指定的输入字符串中搜索正则表达式的所有匹配项。
public:
 System::Text::RegularExpressions::MatchCollection ^ Matches(System::String ^ input);public System.Text.RegularExpressions.MatchCollection Matches (string input);member this.Matches : string -> System.Text.RegularExpressions.MatchCollectionPublic Function Matches (input As String) As MatchCollection参数
- input
- String
要搜索匹配项的字符串。
返回
搜索操作找到的 Match 对象的集合。 如果未找到匹配项,则此方法将返回一个空集合对象。
例外
              input 为 null。
示例
以下示例使用 Matches(String) 方法标识以“es”结尾的句子中的任何单词。
using System;
using System.Text.RegularExpressions;
public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      Regex rgx = new Regex(pattern);
      string sentence = "Who writes these notes?";
      
      foreach (Match match in rgx.Matches(sentence))
         Console.WriteLine("Found '{0}' at position {1}", 
                           match.Value, match.Index);
   }
}
// The example displays the following output:
//       Found 'writes' at position 4
//       Found 'notes' at position 17
Imports System.Text.RegularExpressions
Module Example
   Public Sub Main()
      Dim pattern As String = "\b\w+es\b"
      Dim rgx As New Regex(pattern)
      Dim sentence As String = "Who writes these notes?"
      
      For Each match As Match In rgx.Matches(sentence)
         Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index)
      Next
   End Sub
End Module
' The example displays the following output:
'       Found 'writes' at position 4
'       Found 'notes' at position 17
正则表达式模式 \b\w+es\b 的定义如下表所示。
| 模式 | 描述 | 
|---|---|
| \b | 在单词边界处开始匹配。 | 
| \w+ | 匹配一个或多个单词字符。 | 
| es | 匹配文本字符串“es”。 | 
| \b | 在单词边界处结束匹配。 | 
注解
方法 Matches(String) 类似于 Match(String) 方法,只不过它返回有关在输入字符串中找到的所有匹配项的信息,而不是单个匹配项。 它等效于以下代码:
Match match = regex.Match(input);
while (match.Success) {
      // Handle match here...
      match = match.NextMatch();
}
Dim match As Match = regex.Match(input)
Do While match.Success
      ' Handle match here...
      match = match.NextMatch()
Loop
集合仅包含匹配项,并在第一个不匹配时终止。
方法搜索的 Matches(String) 正则表达式模式是通过调用其中一个 Regex 类构造函数定义的。 有关可形成正则表达式模式的元素的详细信息,请参阅 正则表达式语言 - 快速参考。
方法 Matches 使用延迟计算来填充返回 MatchCollection 的对象。 访问此集合的成员(如 MatchCollection.Count 和 MatchCollection.CopyTo )会导致立即填充集合。 若要利用延迟计算,应使用 C# 和 For Each...Next等foreach构造在 Visual Basic 中循环访问集合。
由于计算延迟,调用 Matches(String) 方法不会引发 RegexMatchTimeoutException 异常。 但是,当对 MatchCollection 此方法返回的对象执行操作时,如果 MatchTimeout 属性不 Regex.InfiniteMatchTimeout 为 ,并且匹配的操作超过超时间隔,则会引发异常。
另请参阅
适用于
Matches(String, String)
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
- Source:
- Regex.Match.cs
在指定的输入字符串中搜索指定的正则表达式的所有匹配项。
public:
 static System::Text::RegularExpressions::MatchCollection ^ Matches(System::String ^ input, System::String ^ pattern);public static System.Text.RegularExpressions.MatchCollection Matches (string input, string pattern);static member Matches : string * string -> System.Text.RegularExpressions.MatchCollectionPublic Shared Function Matches (input As String, pattern As String) As MatchCollection参数
- input
- String
要搜索匹配项的字符串。
- pattern
- String
要匹配的正则表达式模式。
返回
搜索操作找到的 Match 对象的集合。 如果未找到匹配项,则此方法将返回一个空集合对象。
例外
出现正则表达式分析错误。
              input 或 pattern 为 null。
示例
以下示例使用 Matches(String, String) 方法标识以“es”结尾的句子中的任何单词。
using System;
using System.Text.RegularExpressions;
public class Example
{
   public static void Main()
   {
      string pattern = @"\b\w+es\b";
      string sentence = "Who writes these notes?";
      
      foreach (Match match in Regex.Matches(sentence, pattern))
         Console.WriteLine("Found '{0}' at position {1}", 
                           match.Value, match.Index);
   }
}
// The example displays the following output:
//       Found 'writes' at position 4
//       Found 'notes' at position 17
Imports System.Text.RegularExpressions
Module Example
   Public Sub Main()
      Dim pattern As String = "\b\w+es\b"
      Dim sentence As String = "Who writes these notes?"
      For Each match As Match In Regex.Matches(sentence, pattern)
         Console.WriteLine("Found '{0}' at position {1}", match.Value, match.Index)
      Next
   End Sub
End Module
' The example displays the following output:
'       Found 'writes' at position 4
'       Found 'notes' at position 17
正则表达式模式 \b\w+es\b 的定义如下表所示。
| 模式 | 描述 | 
|---|---|
| \b | 在单词边界处开始匹配。 | 
| \w+ | 匹配一个或多个单词字符。 | 
| es | 匹配文本字符串“es”。 | 
| \b | 在单词边界处结束匹配。 | 
注解
方法 Matches(String, String) 类似于 Match(String, String) 方法,只不过它返回有关在输入字符串中找到的所有匹配项的信息,而不是单个匹配项。 它等效于以下代码:
Match match = Regex.Match(input, pattern);
while (match.Success) {
      // Handle match here...
      match = match.NextMatch();
}
Dim match As Match = Regex.Match(input, pattern)
Do While match.Success
      ' Handle match here...
      match = match.NextMatch()
Loop
静态 Matches 方法等效于使用指定的正则表达式模式构造 Regex 对象并调用实例方法 Matches。
参数 pattern 由正则表达式语言元素组成,这些元素以符号方式描述要匹配的字符串。 有关正则表达式的详细信息,请参阅 .NET 正则表达式 和 正则表达式语言 - 快速参考。
方法 Matches 使用延迟计算来填充返回 MatchCollection 的对象。 访问此集合的成员(如 MatchCollection.Count 和 MatchCollection.CopyTo )会导致立即填充集合。 若要利用延迟计算,应使用 C# 和 For Each...Next等foreach构造在 Visual Basic 中循环访问集合。
由于计算延迟,调用 Matches(String, String) 方法不会引发 RegexMatchTimeoutException 异常。 但是,当对 MatchCollection 此方法返回的对象执行操作时,如果超时间隔由当前应用程序域的“REGEX_DEFAULT_MATCH_TIMEOUT”属性定义,并且匹配的操作超过了此超时间隔,则会引发异常。
调用方说明
此方法在一个间隔后超时,该间隔等于调用它的应用程序域的默认超时值。 如果尚未为应用程序域定义超时值,则使用值 InfiniteMatchTimeout(用于防止方法超时)。 用于检索多个模式匹配 Matches(String, String, RegexOptions, TimeSpan)项的建议静态方法是 ,它允许指定超时间隔。