Regex.Match 方法

定義

在輸入字串搜尋規則運算式的項目,並傳回正確結果為單一 Match 物件。

多載

Match(String, String, RegexOptions)

使用指定的比對選項,在輸入字串中搜尋所指定規則運算式的第一個相符項目。

Match(String)

在指定的輸入字串中,搜尋符合 Regex 建構函式中所指定規則運算式的第一個項目。

Match(String, Int32)

從字串中指定的開始位置開始,在輸入字串中搜尋規則運算式的第一個相符項目。

Match(String, String)

在指定的輸入字串中搜尋所指定規則運算式的第一個相符項目。

Match(String, Int32, Int32)

從指定的開始位置開始並且僅搜尋指定數目的字元,在輸入字串中搜尋規則運算式的第一個相符項目。

Match(String, String, RegexOptions, TimeSpan)

使用指定的比對選項和逾時間隔,在輸入字串中搜尋所指定規則運算式的第一個相符項目。

Match(String, String, RegexOptions)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

使用指定的比對選項,在輸入字串中搜尋所指定規則運算式的第一個相符項目。

public:
 static System::Text::RegularExpressions::Match ^ Match(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options);
public static System.Text.RegularExpressions.Match Match (string input, string pattern, System.Text.RegularExpressions.RegexOptions options);
static member Match : string * string * System.Text.RegularExpressions.RegexOptions -> System.Text.RegularExpressions.Match
Public Shared Function Match (input As String, pattern As String, options As RegexOptions) As Match

參數

input
String

用來搜尋比對的字串。

pattern
String

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

options
RegexOptions

列舉值的位元組合,提供用於比對的選項。

傳回

物件,包含符合之項目的相關資訊。

例外狀況

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

inputpatternnull

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

發生逾時。 如需逾時的詳細資訊,請參閱<備註>一節。

範例

下列範例會定義正則表達式,以字母 “a” 開頭的字組。 它會使用 RegexOptions.IgnoreCase 選項來確保正則表達式會找出開頭為大寫 “a” 和小寫 “a” 的字組。

using System;
using System.Text.RegularExpressions;

namespace Examples
{
    public class Example2
    {
        public static void Main()
        {
            string pattern = @"\ba\w*\b";
            string input = "An extraordinary day dawns with each new day.";
            Match m = Regex.Match(input, pattern, RegexOptions.IgnoreCase);
            if (m.Success)
                Console.WriteLine("Found '{0}' at position {1}.", m.Value, m.Index);
        }
    }
}

// The example displays the following output:
//        Found 'An' at position 0.
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\ba\w*\b"
      Dim input As String = "An extraordinary day dawns with each new day."
      Dim m As Match = Regex.Match(input, pattern, RegexOptions.IgnoreCase)
      If m.Success Then
         Console.WriteLine("Found '{0}' at position {1}.", m.Value, m.Index)
      End If
   End Sub
End Module
' The example displays the following output:
'       Found 'An' at position 0.

規則運算式模式 \ba\w*\b 的解譯方式如下表所示。

模式 描述
\b 開始字緣比對。
a 比對字元 「a」。
\w* 比對零、一或多個字字元。
\b 結束字緣比對。

備註

方法 Match(String, String, RegexOptions) 會傳回符合輸入字串中正則表示式模式的第一個子字串。 如需用來建置正則表示式模式之語言專案的資訊,請參閱 正則表示式語言 - 快速參考

靜態Match(String, String, RegexOptions)方法相當於使用Regex(String, RegexOptions)建構函式建構 Regex 物件,並呼叫 實例Match(String)方法。

pattern 參數包含規則運算式語言項目,以透過符號描述要比對的字串。 如需正則表達式的詳細資訊,請參閱 .NET 正則表示式正則表達式語言 - 快速參考

您可以藉由檢查傳 Match 回物件的 Success 屬性值,判斷在輸入字串中找到正規表達式模式。 如果找到符合的項目,則所傳回 Match 物件的 Value 屬性會包含符合規則運算式模式之 input 的子字串。 如果找不到相符專案,其值為 String.Empty

這個方法會傳回 中 input 找到的第一個子字串,該子字串符合正則表達式模式。 您可以重複呼叫傳 Match 回物件的 方法,以擷取後續相符 NextMatch 專案。 您也可以藉由呼叫 Regex.Matches(String, String, RegexOptions) 方法來擷取單一方法呼叫中的所有相符專案。

如果比對作業的執行時間超出為呼叫方法的應用程式定義域指定的逾時間隔,就會擲回 RegexMatchTimeoutException 例外狀況。 如果在應用程式定義域的屬性中未定義任何逾時,或逾時值是 Regex.InfiniteMatchTimeout,則不擲回任何例外狀況。

給呼叫者的注意事項

這個方法的逾時間隔等於呼叫這個方法的應用程式定義域的預設逾時值。 如果未針對應用程式定義域定義逾時值,則使用值 InfiniteMatchTimeout,使方法不會逾時。 用來擷取模式比 Match(String, String)對的建議靜態方法是 ,可讓您設定超時時間間隔。

另請參閱

適用於

Match(String)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

在指定的輸入字串中,搜尋符合 Regex 建構函式中所指定規則運算式的第一個項目。

public:
 System::Text::RegularExpressions::Match ^ Match(System::String ^ input);
public System.Text.RegularExpressions.Match Match (string input);
member this.Match : string -> System.Text.RegularExpressions.Match
Public Function Match (input As String) As Match

參數

input
String

用來搜尋比對的字串。

傳回

物件,包含符合之項目的相關資訊。

例外狀況

inputnull

發生逾時。 如需逾時的詳細資訊,請參閱<備註>一節。

範例

下列範例會尋找字串中的正則表示式模式相符專案,然後列出相符的群組、擷取和擷取位置。

#using <System.dll>

using namespace System;
using namespace System::Text::RegularExpressions;
void main()
{
   
   String^ text = "One car red car blue car";
   String^ pat = "(\\w+)\\s+(car)";
   
   // Compile the regular expression.
   Regex^ r = gcnew Regex( pat,RegexOptions::IgnoreCase );
   
   // Match the regular expression pattern against a text string.
   Match^ m = r->Match(text);
   int matchCount = 0;
   while ( m->Success )
   {
      Console::WriteLine( "Match{0}", ++matchCount );
      for ( int i = 1; i <= 2; i++ )
      {
         Group^ g = m->Groups[ i ];
         Console::WriteLine( "Group{0}='{1}'", i, g );
         CaptureCollection^ cc = g->Captures;
         for ( int j = 0; j < cc->Count; j++ )
         {
            Capture^ c = cc[ j ];
            System::Console::WriteLine( "Capture{0}='{1}', Position={2}", j, c, c->Index );
         }
      }
      m = m->NextMatch();
   }
}  
// This example displays the following output:
//       Match1
//       Group1='One'
//       Capture0='One', Position=0
//       Group2='car'
//       Capture0='car', Position=4
//       Match2
//       Group1='red'
//       Capture0='red', Position=8
//       Group2='car'
//       Capture0='car', Position=12
//       Match3
//       Group1='blue'
//       Capture0='blue', Position=16
//       Group2='car'
//       Capture0='car', Position=21
using System;
using System.Text.RegularExpressions;

class Example
{
   static void Main()
   {
      string text = "One car red car blue car";
      string pat = @"(\w+)\s+(car)";

      // Instantiate the regular expression object.
      Regex r = new Regex(pat, RegexOptions.IgnoreCase);

      // Match the regular expression pattern against a text string.
      Match m = r.Match(text);
      int matchCount = 0;
      while (m.Success)
      {
         Console.WriteLine("Match"+ (++matchCount));
         for (int i = 1; i <= 2; i++)
         {
            Group g = m.Groups[i];
            Console.WriteLine("Group"+i+"='" + g + "'");
            CaptureCollection cc = g.Captures;
            for (int j = 0; j < cc.Count; j++)
            {
               Capture c = cc[j];
               System.Console.WriteLine("Capture"+j+"='" + c + "', Position="+c.Index);
            }
         }
         m = m.NextMatch();
      }
   }
}
// This example displays the following output:
//       Match1
//       Group1='One'
//       Capture0='One', Position=0
//       Group2='car'
//       Capture0='car', Position=4
//       Match2
//       Group1='red'
//       Capture0='red', Position=8
//       Group2='car'
//       Capture0='car', Position=12
//       Match3
//       Group1='blue'
//       Capture0='blue', Position=16
//       Group2='car'
//       Capture0='car', Position=21
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim text As String = "One car red car blue car"
      Dim pattern As String = "(\w+)\s+(car)"

      ' Instantiate the regular expression object.
      Dim r As Regex = new Regex(pattern, RegexOptions.IgnoreCase)

      ' Match the regular expression pattern against a text string.
      Dim m As Match = r.Match(text)
      Dim matchcount as Integer = 0
      Do While m.Success
         matchCount += 1
         Console.WriteLine("Match" & (matchCount))
         Dim i As Integer
         For i = 1 to 2
            Dim g as Group = m.Groups(i)
            Console.WriteLine("Group" & i & "='" & g.ToString() & "'")
            Dim cc As CaptureCollection = g.Captures
            Dim j As Integer 
            For j = 0 to cc.Count - 1
              Dim c As Capture = cc(j)
               Console.WriteLine("Capture" & j & "='" & c.ToString() _
                  & "', Position=" & c.Index)
            Next 
         Next 
         m = m.NextMatch()
      Loop
   End Sub
End Module
' This example displays the following output:
'       Match1
'       Group1='One'
'       Capture0='One', Position=0
'       Group2='car'
'       Capture0='car', Position=4
'       Match2
'       Group1='red'
'       Capture0='red', Position=8
'       Group2='car'
'       Capture0='car', Position=12
'       Match3
'       Group1='blue'
'       Capture0='blue', Position=16
'       Group2='car'
'       Capture0='car', Position=21

正則表達式模式 (\w+)\s+(car) 會比對 「car」 一詞的出現次數,以及前面出現的字組。 其解譯方式如下表所示。

模式 描述
(\w+) 比對一個或多個文字字元。 這是第一個擷取群組。
\s+ 比對一個或多個空白字元。
(汽車) 比對常值字串 「car」。 這是第二個擷取群組。

備註

方法 Match(String) 會傳回符合輸入字串中正則表示式模式的第一個子字串。 如需用來建置正則表示式模式之語言專案的資訊,請參閱 正則表示式語言 - 快速參考

您可以藉由檢查傳 Match 回物件的 Success 屬性值,判斷在輸入字串中找到正規表達式模式。 如果找到符合的項目,則所傳回 Match 物件的 Value 屬性會包含符合規則運算式模式之 input 的子字串。 如果找不到相符專案,其值為 String.Empty

這個方法會傳回 中 input 符合正則表達式模式的第一個子字串。 您可以重複呼叫傳 Match 回物件的 方法,以擷取後續相符 Match.NextMatch 專案。 您也可以藉由呼叫 Regex.Matches(String) 方法來擷取單一方法呼叫中的所有相符專案。

如果比對作業的執行時間超出 RegexMatchTimeoutException 建構函式所指定的逾時間隔,就會擲回 Regex.Regex(String, RegexOptions, TimeSpan) 例外狀況。 呼叫建構函式時若未設定逾時間隔,則如果作業超過為建立 Regex 物件的應用程式定義域設定的任何逾時值,就會擲回例外狀況。 如果在 Regex 建構函式呼叫或應用程式定義域的屬性中未定義任何逾時,或逾時值是 Regex.InfiniteMatchTimeout,則不擲回任何例外狀況。

另請參閱

適用於

Match(String, Int32)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

從字串中指定的開始位置開始,在輸入字串中搜尋規則運算式的第一個相符項目。

public:
 System::Text::RegularExpressions::Match ^ Match(System::String ^ input, int startat);
public System.Text.RegularExpressions.Match Match (string input, int startat);
member this.Match : string * int -> System.Text.RegularExpressions.Match
Public Function Match (input As String, startat As Integer) As Match

參數

input
String

用來搜尋比對的字串。

startat
Int32

要開始搜尋之以零起始的字元位置。

傳回

物件,包含符合之項目的相關資訊。

例外狀況

inputnull

startat 小於零或大於 input 的長度。

發生逾時。 如需逾時的詳細資訊,請參閱<備註>一節。

備註

如需此 API 的詳細資訊,請參閱 Regex.Match 的補充 API 備註

另請參閱

適用於

Match(String, String)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

在指定的輸入字串中搜尋所指定規則運算式的第一個相符項目。

public:
 static System::Text::RegularExpressions::Match ^ Match(System::String ^ input, System::String ^ pattern);
public static System.Text.RegularExpressions.Match Match (string input, string pattern);
static member Match : string * string -> System.Text.RegularExpressions.Match
Public Shared Function Match (input As String, pattern As String) As Match

參數

input
String

用來搜尋比對的字串。

pattern
String

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

傳回

物件,包含符合之項目的相關資訊。

例外狀況

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

inputpatternnull

發生逾時。 如需逾時的詳細資訊,請參閱<備註>一節。

範例

下列範例會 Match(String, String) 呼叫 方法來尋找包含至少一個字元的第一個 z 單字,然後呼叫 Match.NextMatch 方法來尋找任何其他相符專案。

using System;
using System.Text.RegularExpressions;

namespace Examples
{
    public class Example
    {
        public static void Main()
        {
            string input = "ablaze beagle choral dozen elementary fanatic " +
                           "glaze hunger inept jazz kitchen lemon minus " +
                           "night optical pizza quiz restoration stamina " +
                           "train unrest vertical whiz xray yellow zealous";
            string pattern = @"\b\w*z+\w*\b";
            Match m = Regex.Match(input, pattern);
            while (m.Success)
            {
                Console.WriteLine("'{0}' found at position {1}", m.Value, m.Index);
                m = m.NextMatch();
            }
        }
    }
}

// The example displays the following output:
//    'ablaze' found at position 0
//    'dozen' found at position 21
//    'glaze' found at position 46
//    'jazz' found at position 65
//    'pizza' found at position 104
//    'quiz' found at position 110
//    'whiz' found at position 157
//    'zealous' found at position 174
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim input As String = "ablaze beagle choral dozen elementary fanatic " +
                            "glaze hunger inept jazz kitchen lemon minus " +
                            "night optical pizza quiz restoration stamina " +
                            "train unrest vertical whiz xray yellow zealous"
      Dim pattern As String = "\b\w*z+\w*\b"
      Dim m As Match = Regex.Match(input, pattern)
      Do While m.Success 
         Console.WriteLine("'{0}' found at position {1}", m.Value, m.Index)
         m = m.NextMatch()
      Loop                      
   End Sub
End Module
' The example displays the following output:
    'ablaze' found at position 0
    'dozen' found at position 21
    'glaze' found at position 46
    'jazz' found at position 65
    'pizza' found at position 104
    'quiz' found at position 110
    'whiz' found at position 157
    'zealous' found at position 174

規則運算式模式 \b\w*z+\w*\b 的解譯方式如下表所示。

模式 描述
\b 開始字緣比對。
\w* 比對零、一或多個字字元。
z+ 比對一或多個出現的 z 字元。
\w* 比對零、一或多個文字字元。
\b 結束字緣比對。

備註

方法 Match(String, String) 會傳回符合輸入字串中正則表示式模式的第一個子字串。 如需用來建立正則表示式模式之語言專案的資訊,請參閱 正則表示式語言 - 快速參考

靜態 Match(String, String) 方法相當於使用指定的正則表達式模式建構 Regex 物件,並呼叫 實例 Match(String) 方法。 在此情況下,正則表達式引擎會快取正則表示式模式。

pattern 參數包含規則運算式語言項目,以透過符號描述要比對的字串。 如需正則表達式的詳細資訊,請參閱 .NET 正則表示式正則表達式語言 - 快速參考

您可以藉由檢查傳 Match 回物件的 Success 屬性值,判斷在輸入字串中是否找到正規表達式模式。 如果找到符合的項目,則所傳回 Match 物件的 Value 屬性會包含符合規則運算式模式之 input 的子字串。 如果找不到相符專案,其值為 String.Empty

這個方法會傳回 中 input 符合正則表達式模式的第一個子字串。 您可以重複呼叫傳 Match 回物件的 方法,以擷取後續相符專案 Match.NextMatch 。 您也可以呼叫 Regex.Matches(String, String) 方法來擷取單一方法呼叫中的所有相符專案。

如果比對作業的執行時間超出為呼叫方法的應用程式定義域指定的逾時間隔,就會擲回 RegexMatchTimeoutException 例外狀況。 如果在應用程式定義域的屬性中未定義任何逾時,或逾時值是 Regex.InfiniteMatchTimeout,則不擲回任何例外狀況。

給呼叫者的注意事項

這個方法的逾時間隔等於呼叫這個方法的應用程式定義域的預設逾時值。 如果未針對應用程式定義域定義逾時值,則使用值 InfiniteMatchTimeout,使方法不會逾時。 用來擷取模式比 Match(String, String)對的建議靜態方法是 ,可讓您設定超時時間間隔。

另請參閱

適用於

Match(String, Int32, Int32)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

從指定的開始位置開始並且僅搜尋指定數目的字元,在輸入字串中搜尋規則運算式的第一個相符項目。

public:
 System::Text::RegularExpressions::Match ^ Match(System::String ^ input, int beginning, int length);
public System.Text.RegularExpressions.Match Match (string input, int beginning, int length);
member this.Match : string * int * int -> System.Text.RegularExpressions.Match
Public Function Match (input As String, beginning As Integer, length As Integer) As Match

參數

input
String

用來搜尋比對的字串。

beginning
Int32

定義要搜尋的最左邊位置的輸入字串中以零為起始的字元位置。

length
Int32

子字串中要包含在搜尋中的字元數。

傳回

物件,包含符合之項目的相關資訊。

例外狀況

inputnull

beginning 小於零或大於 input 的長度。

-或-

length 小於零或大於 input 的長度。

-或-

beginning+length-1 識別 input 範圍之外的位置。

發生逾時。 如需逾時的詳細資訊,請參閱<備註>一節。

備註

方法 Match(String, Int32, Int32) 會傳回符合輸入字串一部分之正則表示式模式的第一個子字串。 如需用來建立正則表示式模式之語言專案的資訊,請參閱 正則表示式語言 - 快速參考

方法搜尋的正則表示式模式 Match(String, Int32, Int32) 是由呼叫其中 Regex 一個類別建構函式所定義。 如需可形成正則表示式模式之元素的詳細資訊,請參閱 正則表示式語言 - 快速參考

方法會Match(String, Int32, Int32)搜尋 和 length 參數所beginning定義的部分input,以取得正則表達式模式。 beginning 一律會定義要包含在搜尋中最左邊字元的索引,並 length 定義要搜尋的最大字元數。 它們會一起定義搜尋的範圍。 此行為與 input 實際上 input.Substring(beginning, length)完全相同,不同之處在於,任何相符專案的索引都會相對於的 input開頭計算。 這表示模式開頭或結尾的任何錨點或零寬度判斷提示的行為就像沒有超出此範圍一樣 input 。 例如,錨點 、 和 將會在 $beginning 和 上滿足,而且\z會在 上滿足。\A\G^beginning + length - 1

如果搜尋從左至右 (預設) ,則正則表示式引擎會從索引 beginning 處的字元搜尋到索引 beginning + length - 1處的字元。 如果使用 選項具現化RegexOptions.RightToLeft正則表達式引擎,讓搜尋從右至左進行,則正則表達式引擎會從索引beginning + length - 1beginning處的字元搜尋索引處的字元。

這個方法會傳回它在此範圍內找到的第一個相符專案。 您可以重複呼叫傳 Match 回物件的 方法,以擷取後續相符專案 Match.NextMatch

您可以藉由檢查傳 Match 回物件的 Success 屬性值,判斷在輸入字串中是否找到正規表達式模式。 如果找到符合的項目,則所傳回 Match 物件的 Value 屬性會包含符合規則運算式模式之 input 的子字串。 如果找不到相符專案,其值為 String.Empty

如果比對作業的執行時間超出 RegexMatchTimeoutException 建構函式所指定的逾時間隔,就會擲回 Regex.Regex(String, RegexOptions, TimeSpan) 例外狀況。 如果您在呼叫建構函式時未設定逾時值,如果作業超過為建立對象的應用程式域 Regex 所建立的任何逾時值,就會擲回例外狀況。 如果在 Regex 建構函式呼叫或應用程式定義域的屬性中未定義任何逾時,或逾時值是 Regex.InfiniteMatchTimeout,則不擲回任何例外狀況。

另請參閱

適用於

Match(String, String, RegexOptions, TimeSpan)

Source:
Regex.Match.cs
Source:
Regex.Match.cs
Source:
Regex.Match.cs

使用指定的比對選項和逾時間隔,在輸入字串中搜尋所指定規則運算式的第一個相符項目。

public:
 static System::Text::RegularExpressions::Match ^ Match(System::String ^ input, System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options, TimeSpan matchTimeout);
public static System.Text.RegularExpressions.Match Match (string input, string pattern, System.Text.RegularExpressions.RegexOptions options, TimeSpan matchTimeout);
static member Match : string * string * System.Text.RegularExpressions.RegexOptions * TimeSpan -> System.Text.RegularExpressions.Match
Public Shared Function Match (input As String, pattern As String, options As RegexOptions, matchTimeout As TimeSpan) As Match

參數

input
String

用來搜尋比對的字串。

pattern
String

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

options
RegexOptions

列舉值的位元組合,提供用於比對的選項。

matchTimeout
TimeSpan

逾時間隔,若要表示此方法不應逾時則為 InfiniteMatchTimeout

傳回

物件,包含符合之項目的相關資訊。

例外狀況

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

inputpatternnull

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

-或-

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

發生逾時。 如需逾時的詳細資訊,請參閱<備註>一節。

備註

方法 Match(String, String, RegexOptions, TimeSpan) 會傳回符合輸入字串中正則表示式模式的第一個子字串。 如需用來建立正則表示式模式之語言專案的資訊,請參閱 正則表示式語言 - 快速參考

靜態Match(String, String, RegexOptions, TimeSpan)方法相當於使用Regex(String, RegexOptions, TimeSpan)建構函式建構 Regex 物件,並呼叫 實例Match(String)方法。

pattern 參數包含規則運算式語言項目,以透過符號描述要比對的字串。 如需正則表達式的詳細資訊,請參閱 .NET 正則表示式正則表達式語言 - 快速參考

您可以藉由檢查傳 Match 回物件的 Success 屬性值,判斷在輸入字串中是否找到正規表達式模式。 如果找到符合的項目,則所傳回 Match 物件的 Value 屬性會包含符合規則運算式模式之 input 的子字串。 如果找不到相符專案,其值為 String.Empty

這個方法會傳回 中 input 找到的第一個符合正則表達式模式的子字串。 您可以重複呼叫傳 Match 回物件的 方法,以擷取後續相符專案 NextMatch 。 您也可以呼叫 Regex.Matches(String, String, RegexOptions) 方法來擷取單一方法呼叫中的所有相符專案。

參數 matchTimeout 會指定模式比對方法在逾時之前應該嘗試尋找相符項目的時間長度。設定超時時間間隔可防止依賴過多回溯的正則表達式在處理包含接近相符項目的輸入時停止回應。 如需詳細資訊,請參閱正則表達式和回溯的最佳做法。 如果該時間間隔中找不到相符專案,方法會 RegexMatchTimeoutException 擲回例外狀況。 matchTimeout 會覆寫針對方法執行所在應用程式域所定義的任何預設逾時值。

給呼叫者的注意事項

我們建議您將 matchTimeout 參數設定為適當的值,例如兩秒。 如果您藉由指定 InfiniteMatchTimeout來停用逾時,正則表達式引擎會提供稍微更好的效能。 不過,您應該只在下列情況下停用逾時:

  • 當正則表達式處理的輸入衍生自已知且受信任的來源,或由靜態文字所組成時。 這不包括使用者動態輸入的文字。

  • 當正則表達式模式經過徹底測試,以確保其有效率地處理相符專案、非相符專案和接近相符專案。

  • 當正則表達式模式不包含已知在處理接近相符專案時造成過多回溯的語言專案時。

另請參閱

適用於