Regex.Matches 方法

定義

在輸入字串中搜尋規則運算式的所有項目,並傳回所有符合項目。

多載

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.MatchCollection
Public 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 物件集合。 如果找不到相符的項目,此方法會傳回空集合物件。

例外狀況

發生規則運算式剖析錯誤。

inputpatternnull

options 不是 RegexOptions 值的有效位元組合。

-或-

matchTimeout 為負數、零或約大於 24 天。

範例

下列範例會 Matches(String, String, RegexOptions, TimeSpan) 呼叫 方法來執行區分大小寫的比較,此比較會比對結尾為 「es」 之句子中的任何單字。 然後它會 Matches(String, String, RegexOptions, TimeSpan) 呼叫 方法來執行不區分大小寫的模式與輸入字串的比較。 在這兩種情況下,逾時間隔會設定為一秒。 如輸出所示,這兩種方法會傳回不同的結果。

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.CountMatchCollection.CopyTo) 會導致立即填入集合。 若要利用延遲評估,您應該使用 C# 中的 For Eachforeach 構和 Visual Basic 中的 ... Next 來逐一查看集合。

由於其延遲評估,呼叫 Matches 方法不會擲回例外狀況 RegexMatchTimeoutException 。 不過,如果在此方法傳回的物件上 MatchCollection 執行作業時,如果相符的作業超過 參數指定的這個逾時間隔,就會擲回例外狀況 matchTimeout

給呼叫者的注意事項

我們建議您將 matchTimeout 參數設定為適當的值,例如兩秒。 如果您藉由指定 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.MatchCollection
Public Shared Function Matches (input As String, pattern As String, options As RegexOptions) As MatchCollection

參數

input
String

用來搜尋比對的字串。

pattern
String

要比對的規則運算式模式。

options
RegexOptions

列舉值的位元組合,這些值會指定用於比對的選項。

傳回

搜尋之後找到的 Match 物件集合。 如果找不到相符的項目,此方法會傳回空集合物件。

例外狀況

發生規則運算式剖析錯誤。

inputpatternnull

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.CountMatchCollection.CopyTo) 會導致立即填入集合。 若要利用延遲評估,您應該使用 C# 中的 For Eachforeach 構和 Visual Basic 中的 ... Next 來逐一查看集合。

由於其延遲評估,呼叫 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.MatchCollection
Public Function Matches (input As String, startat As Integer) As MatchCollection

參數

input
String

用來搜尋比對的字串。

startat
Int32

在輸入字串中開始搜尋的字元位置。

傳回

搜尋之後找到的 Match 物件集合。 如果找不到相符的項目,此方法會傳回空集合物件。

例外狀況

inputnull

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.CountMatchCollection.CopyTo) 會導致立即填入集合。 若要利用延遲評估,您應該使用 C# 和 For Each Visual Basic 中的 ... Nextforeach 建構來逐一查看集合。

由於其延遲評估,呼叫 Matches(String, Int32) 方法不會擲回 RegexMatchTimeoutException 例外狀況。 不過,如果 MatchTimeout 屬性不是 Regex.InfiniteMatchTimeout ,且相符的作業超過逾時間隔,則會在此方法傳回的物件上 MatchCollection 執行作業時擲回例外狀況。

另請參閱

適用於

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.MatchCollection
Public Function Matches (input As String) As MatchCollection

參數

input
String

用來搜尋比對的字串。

傳回

搜尋之後找到的 Match 物件集合。 如果找不到相符的項目,此方法會傳回空集合物件。

例外狀況

inputnull

範例

下列範例會 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.CountMatchCollection.CopyTo) 會導致立即填入集合。 若要利用延遲評估,您應該使用 C# 和 For Each Visual Basic 中的 ... Nextforeach 建構來逐一查看集合。

由於其延遲評估,呼叫 Matches(String) 方法不會擲回 RegexMatchTimeoutException 例外狀況。 不過,如果 MatchTimeout 屬性不是 Regex.InfiniteMatchTimeout ,且相符的作業超過逾時間隔,則會在此方法傳回的物件上 MatchCollection 執行作業時擲回例外狀況。

另請參閱

適用於

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.MatchCollection
Public Shared Function Matches (input As String, pattern As String) As MatchCollection

參數

input
String

用來搜尋比對的字串。

pattern
String

要比對的規則運算式模式。

傳回

搜尋之後找到的 Match 物件集合。 如果找不到相符的項目,此方法會傳回空集合物件。

例外狀況

發生規則運算式剖析錯誤。

inputpatternnull

範例

下列範例會 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.CountMatchCollection.CopyTo) 會導致立即填入集合。 若要利用延遲評估,您應該使用 C# 和 For Each Visual Basic 中的 ... Nextforeach 建構來逐一查看集合。

由於其延遲評估,呼叫 Matches(String, String) 方法不會擲回 RegexMatchTimeoutException 例外狀況。 不過,如果在此方法傳回的物件上 MatchCollection 執行作業時,就會擲回例外狀況,如果逾時間隔是由目前應用程式域的 「REGEX_DEFAULT_MATCH_TIMEOUT」 屬性所定義,且相符的作業超過這個逾時間隔。

給呼叫者的注意事項

這個方法的逾時間隔等於呼叫這個方法的應用程式定義域的預設逾時值。 如果未針對應用程式定義域定義逾時值,則使用值 InfiniteMatchTimeout,使方法不會逾時。 擷取多個模式相符 Matches(String, String, RegexOptions, TimeSpan) 專案的建議靜態方法是 ,可讓您指定逾時間隔。

另請參閱

適用於