Regex Regex Regex Regex Constructors

定義

多載

Regex() Regex() Regex()

初始化 Regex 類別的新執行個體。Initializes a new instance of the Regex class.

Regex(String) Regex(String) Regex(String) Regex(String)

為指定的規則運算式初始化 Regex 類別的新執行個體。Initializes a new instance of the Regex class for the specified regular expression.

Regex(SerializationInfo, StreamingContext) Regex(SerializationInfo, StreamingContext) Regex(SerializationInfo, StreamingContext) Regex(SerializationInfo, StreamingContext)

使用序列化的資料,初始化 Regex 類別的新執行個體。Initializes a new instance of the Regex class by using serialized data.

Regex(String, RegexOptions) Regex(String, RegexOptions) Regex(String, RegexOptions) Regex(String, RegexOptions)

使用會修改模式的選項,為指定的規則運算式初始化 Regex 類別的新執行個體。Initializes a new instance of the Regex class for the specified regular expression, with options that modify the pattern.

Regex(String, RegexOptions, TimeSpan) Regex(String, RegexOptions, TimeSpan) Regex(String, RegexOptions, TimeSpan) Regex(String, RegexOptions, TimeSpan)

針對指定的規則運算式,使用修改模式的選項,以及指定在逾時前模式比對方法應該嘗試比對的時間長度的值,初始化 Regex 類別的新執行個體。Initializes a new instance of the Regex class for the specified regular expression, with options that modify the pattern and a value that specifies how long a pattern matching method should attempt a match before it times out.

Regex() Regex() Regex()

初始化 Regex 類別的新執行個體。Initializes a new instance of the Regex class.

protected:
 Regex();
protected Regex ();
Protected Sub New ()

備註

請注意, 這個函數會受到保護;它只能由衍生自類別的Regex類別呼叫。Note that this constructor is protected; it can only be called by classes derived from the Regex class.

Regex(String) Regex(String) Regex(String) Regex(String)

為指定的規則運算式初始化 Regex 類別的新執行個體。Initializes a new instance of the Regex class for the specified regular expression.

public:
 Regex(System::String ^ pattern);
public Regex (string pattern);
new System.Text.RegularExpressions.Regex : string -> System.Text.RegularExpressions.Regex
Public Sub New (pattern As String)

參數

pattern
String String String String

要比對的規則運算式模式。The regular expression pattern to match.

例外狀況

發生規則運算式剖析錯誤。A regular expression parsing error occurred.

範例

下列範例說明如何使用此函式來具現化正則運算式, 以符合開頭為字母 "a" 或 "t" 的任何字組。The following example illustrates how to use this constructor to instantiate a regular expression that matches any word that begins with the letters "a" or "t".

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b[at]\w+";
      string text = "The threaded application ate up the thread pool as it executed.";
      MatchCollection matches;

      Regex defaultRegex = new Regex(pattern);
      // Get matches of pattern in text
      matches = defaultRegex.Matches(text);
      Console.WriteLine("Parsing '{0}'", text);
      // Iterate matches
      for (int ctr = 0; ctr < matches.Count; ctr++)
         Console.WriteLine("{0}. {1}", ctr, matches[ctr].Value);
   }
}
// The example displays the following output:
//       Parsing 'The threaded application ate up the thread pool as it executed.'
//       0. threaded
//       1. application
//       2. ate
//       3. the
//       4. thread
//       5. as
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b[at]\w+"
      Dim text As String = "The threaded application ate up the thread pool as it executed."
      Dim matches As MatchCollection

      Dim defaultRegex As New Regex(pattern)
      ' Get matches of pattern in text
      matches = defaultRegex.Matches(text)
      Console.WriteLine("Parsing '{0}'", text)
      ' Iterate matches
      For ctr As Integer = 0 to matches.Count - 1
         Console.WriteLine("{0}. {1}", ctr, matches(ctr).Value)
      Next
   End Sub
End Module
' The example displays the following output:
'       Parsing 'The threaded application ate up the thread pool as it executed.'
'       0. threaded
'       1. application
'       2. ate
'       3. the
'       4. thread
'       5. as

請注意, 正則運算式模式不符合文字開頭的 "the" 一字, 因為根據預設, 比較會區分大小寫。Note that the regular expression pattern cannot match the word "The" at the beginning of the text, because comparisons are case-sensitive by default. 如需比較不區分大小寫的範例, 請Regex(String, RegexOptions)參閱函數。For an example of case-insensitive comparison, see the Regex(String, RegexOptions) constructor.

備註

pattern 參數包含規則運算式語言項目,以透過符號描述要比對的字串。The pattern parameter consists of regular expression language elements that symbolically describe the string to match. 如需正則運算式的詳細資訊, 請參閱.NET Framework 正則運算式正則運算式語言-快速參考主題。For more information about regular expressions, see the .NET Framework Regular Expressions and Regular Expression Language - Quick Reference topics.

呼叫此Regex(String)函式相當於針對optionsRegex(String, RegexOptions)變數呼叫具有值的None函式。Calling the Regex(String) constructor is equivalent to calling the Regex(String, RegexOptions) constructor with a value of None for the options argument.

Regex物件是不可變的, 這表示它只能用於您在建立時所定義的比對模式。A Regex object is immutable, which means that it can be used only for the match pattern you define when you create it. 不過, 不需要重新編譯, 就可以使用任何次數。However, it can be used any number of times without being recompiled.

這個函式會具現化正則運算式物件, 其會嘗試區分大小寫符合中pattern所定義的任何字母字元。This constructor instantiates a regular expression object that attempts a case-sensitive match of any alphabetical characters defined in pattern. 若為不區分大小寫的比對Regex.Regex(String, RegexOptions) , 請使用此函數。For a case-insensitive match, use the Regex.Regex(String, RegexOptions) constructor.

給呼叫者的注意事項

此函式會Regex建立物件, 該物件會使用建立它之應用程式域的預設超時值。This constructor creates a Regex object that uses the default time-out value of the application domain in which it is created. 如果尚未定義應用程式域的超時值, Regex物件會使用值InfiniteMatchTimeout, 以防止作業超時。建立Regex物件的建議函式為Regex(String, RegexOptions, TimeSpan), 可讓您設定逾時間隔。If a time-out value has not been defined for the application domain, the Regex object uses the value InfiniteMatchTimeout, which prevents the operation from timing out. The recommended constructor for creating a Regex object is Regex(String, RegexOptions, TimeSpan), which lets you set the time-out interval.

另請參閱

Regex(SerializationInfo, StreamingContext) Regex(SerializationInfo, StreamingContext) Regex(SerializationInfo, StreamingContext) Regex(SerializationInfo, StreamingContext)

使用序列化的資料,初始化 Regex 類別的新執行個體。Initializes a new instance of the Regex class by using serialized data.

protected:
 Regex(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
protected Regex (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new System.Text.RegularExpressions.Regex : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> System.Text.RegularExpressions.Regex
Protected Sub New (info As SerializationInfo, context As StreamingContext)

參數

info
SerializationInfo SerializationInfo SerializationInfo SerializationInfo

包含序列化模式和 RegexOptions 資訊的物件。The object that contains a serialized pattern and RegexOptions information.

context
StreamingContext StreamingContext StreamingContext StreamingContext

這個序列化的目的端。The destination for this serialization. (不使用這個參數;請指定 null)。(This parameter is not used; specify null.)

例外狀況

發生規則運算式剖析錯誤。A regular expression parsing error occurred.

info 包含的模式為 nullThe pattern that info contains is null.

Regex(String, RegexOptions) Regex(String, RegexOptions) Regex(String, RegexOptions) Regex(String, RegexOptions)

使用會修改模式的選項,為指定的規則運算式初始化 Regex 類別的新執行個體。Initializes a new instance of the Regex class for the specified regular expression, with options that modify the pattern.

public:
 Regex(System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options);
public Regex (string pattern, System.Text.RegularExpressions.RegexOptions options);
new System.Text.RegularExpressions.Regex : string * System.Text.RegularExpressions.RegexOptions -> System.Text.RegularExpressions.Regex
Public Sub New (pattern As String, options As RegexOptions)

參數

pattern
String String String String

要比對的規則運算式模式。The regular expression pattern to match.

options
RegexOptions RegexOptions RegexOptions RegexOptions

列舉值的位元組合,這些值會修改規則運算式。A bitwise combination of the enumeration values that modify the regular expression.

例外狀況

發生規則運算式剖析錯誤。A regular expression parsing error occurred.

範例

下列範例說明如何使用此函式來具現化正則運算式, 以符合開頭為字母 "a" 或 "t" 的任何字組。The following example illustrates how to use this constructor to instantiate a regular expression that matches any word that begins with the letters "a" or "t".

using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\b[at]\w+";
      RegexOptions options = RegexOptions.IgnoreCase | RegexOptions.Compiled;
      string text = "The threaded application ate up the thread pool as it executed.";
      MatchCollection matches;

      Regex optionRegex = new Regex(pattern, options);
      Console.WriteLine("Parsing '{0}' with options {1}:", text, options.ToString());
      // Get matches of pattern in text
      matches = optionRegex.Matches(text);
      // Iterate matches
      for (int ctr = 0; ctr < matches.Count; ctr++)
         Console.WriteLine("{0}. {1}", ctr, matches[ctr].Value);
   }
}
// The example displays the following output:
//    Parsing 'The threaded application ate up the thread pool as it executed.'
//        with options IgnoreCase, Compiled:
//    0. The
//    1. threaded
//    2. application
//    3. ate
//    4. the
//    5. thread
//    6. as
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim pattern As String = "\b[at]\w+"
      Dim options As RegexOptions = RegexOptions.IgnoreCase Or RegexOptions.Compiled
      Dim text As String = "The threaded application ate up the thread pool as it executed."
      Dim matches As MatchCollection

      Dim optionRegex As New Regex(pattern, options)
      Console.WriteLine("Parsing '{0}' with options {1}:", text, options.ToString())
      ' Get matches of pattern in text
      matches = optionRegex.Matches(text)
      ' Iterate matches   
      For ctr As Integer = 0 to matches.Count - 1
         Console.WriteLine("{0}. {1}", ctr, matches(ctr).Value)
      Next
   End Sub
End Module
' The example displays the following output:
'    Parsing 'The threaded application ate up the thread pool as it executed.'
'       with options IgnoreCase, Compiled:
'    0. The
'    1. threaded
'    2. application
'    3. ate
'    4. the
'    5. thread
'    6. as

請注意, match 集合包含開始文字的 "the" 一詞, 因為options參數已定義不區分大小寫的比較。Note that the match collection includes the word "The" that begins the text because the options parameter has defined case-insensitive comparisons.

備註

pattern 參數包含規則運算式語言項目,以透過符號描述要比對的字串。The pattern parameter consists of regular expression language elements that symbolically describe the string to match. 如需正則運算式的詳細資訊, 請參閱.NET Framework 正則運算式正則運算式語言-快速參考主題。For more information about regular expressions, see the .NET Framework Regular Expressions and Regular Expression Language - Quick Reference topics.

Regex物件是不可變的, 這表示它只能用於您在建立時所定義的相符參數。A Regex object is immutable, which means that it can be used only for the match parameters you define when you create it. 不過, 不需要重新編譯, 就可以使用任何次數。However, it can be used any number of times without being recompiled.

給呼叫者的注意事項

此函式會Regex建立物件, 該物件會使用建立它之應用程式域的預設超時值。This constructor creates a Regex object that uses the default time-out value of the application domain in which it is created. 如果尚未定義應用程式域的超時值, Regex物件會使用值InfiniteMatchTimeout, 以防止作業超時。建立Regex物件的建議函式為Regex(String, RegexOptions, TimeSpan), 可讓您設定逾時間隔。If a time-out value has not been defined for the application domain, the Regex object uses the value InfiniteMatchTimeout, which prevents the operation from timing out. The recommended constructor for creating a Regex object is Regex(String, RegexOptions, TimeSpan), which lets you set the time-out interval.

另請參閱

Regex(String, RegexOptions, TimeSpan) Regex(String, RegexOptions, TimeSpan) Regex(String, RegexOptions, TimeSpan) Regex(String, RegexOptions, TimeSpan)

針對指定的規則運算式,使用修改模式的選項,以及指定在逾時前模式比對方法應該嘗試比對的時間長度的值,初始化 Regex 類別的新執行個體。Initializes a new instance of the Regex class for the specified regular expression, with options that modify the pattern and a value that specifies how long a pattern matching method should attempt a match before it times out.

public:
 Regex(System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options, TimeSpan matchTimeout);
public Regex (string pattern, System.Text.RegularExpressions.RegexOptions options, TimeSpan matchTimeout);
new System.Text.RegularExpressions.Regex : string * System.Text.RegularExpressions.RegexOptions * TimeSpan -> System.Text.RegularExpressions.Regex
Public Sub New (pattern As String, options As RegexOptions, matchTimeout As TimeSpan)

參數

pattern
String String String String

要比對的規則運算式模式。The regular expression pattern to match.

options
RegexOptions RegexOptions RegexOptions RegexOptions

列舉值的位元組合,這些值會修改規則運算式。A bitwise combination of the enumeration values that modify the regular expression.

matchTimeout
TimeSpan TimeSpan TimeSpan TimeSpan

逾時間隔,若要表示此方法不應逾時則為 InfiniteMatchTimeoutA time-out interval, or InfiniteMatchTimeout to indicate that the method should not time out.

例外狀況

發生規則運算式剖析錯誤。A regular expression parsing error occurred.

options 不是有效的 RegexOptions 值。options is not a valid RegexOptions value.

-或--or- matchTimeout 為負數、零或約大於 24 天。matchTimeout is negative, zero, or greater than approximately 24 days.

範例

下列範例會呼叫此Regex(String, RegexOptions, TimeSpan)函式, 以Regex具現化具有一秒超時值的物件。The following example calls the Regex(String, RegexOptions, TimeSpan) constructor to instantiate a Regex object with a time-out value of one second. 規則運算式模式 (a+)+$會在行尾比對一個或多個 "a" 字元的一個或多個序列,並且受限於大量回溯。The regular expression pattern (a+)+$, which matches one or more sequences of one or more "a" characters at the end of a line, is subject to excessive backtracking. RegexMatchTimeoutException如果擲回, 此範例會將超時值增加到最大值3秒。If a RegexMatchTimeoutException is thrown, the example increases the time-out value up to the maximum value of three seconds. 否則, 它會放棄符合模式的嘗試。Otherwise, it abandons the attempt to match the pattern.

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Security;
using System.Text.RegularExpressions;
using System.Threading; 

public class Example
{
   const int MaxTimeoutInSeconds = 3;

   public static void Main()
   {
      string pattern = @"(a+)+$";    // DO NOT REUSE THIS PATTERN.
      Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1));       
      Stopwatch sw = null;
      
      string[] inputs= { "aa", "aaaa>", 
                         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                         "aaaaaaaaaaaaaaaaaaaaaa>",
                         "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>" };
                                 
      foreach (var inputValue in inputs) {
         Console.WriteLine("Processing {0}", inputValue);
         bool timedOut = false;
         do { 
            try {
               sw = Stopwatch.StartNew();
               // Display the result.
               if (rgx.IsMatch(inputValue)) {
                  sw.Stop();
                  Console.WriteLine(@"Valid: '{0}' ({1:ss\.fffffff} seconds)", 
                                    inputValue, sw.Elapsed); 
               }
               else {
                  sw.Stop();
                  Console.WriteLine(@"'{0}' is not a valid string. ({1:ss\.fffff} seconds)", 
                                    inputValue, sw.Elapsed);
               }
            }
            catch (RegexMatchTimeoutException e) {   
               sw.Stop();
               // Display the elapsed time until the exception.
               Console.WriteLine(@"Timeout with '{0}' after {1:ss\.fffff}", 
                                 inputValue, sw.Elapsed);
               Thread.Sleep(1500);       // Pause for 1.5 seconds.

               // Increase the timeout interval and retry.
               TimeSpan timeout = e.MatchTimeout.Add(TimeSpan.FromSeconds(1));
               if (timeout.TotalSeconds > MaxTimeoutInSeconds) {
                  Console.WriteLine("Maximum timeout interval of {0} seconds exceeded.",
                                    MaxTimeoutInSeconds);
                  timedOut = false;
               }
               else {               
                  Console.WriteLine("Changing the timeout interval to {0}", 
                                    timeout); 
                  rgx = new Regex(pattern, RegexOptions.IgnoreCase, timeout);
                  timedOut = true;
               }
            }
         } while (timedOut);
         Console.WriteLine();
      }   
   }
}
// The example displays output like the following :
//    Processing aa
//    Valid: 'aa' (00.0000779 seconds)
//    
//    Processing aaaa>
//    'aaaa>' is not a valid string. (00.00005 seconds)
//    
//    Processing aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
//    Valid: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' (00.0000043 seconds)
//    
//    Processing aaaaaaaaaaaaaaaaaaaaaa>
//    Timeout with 'aaaaaaaaaaaaaaaaaaaaaa>' after 01.00469
//    Changing the timeout interval to 00:00:02
//    Timeout with 'aaaaaaaaaaaaaaaaaaaaaa>' after 02.01202
//    Changing the timeout interval to 00:00:03
//    Timeout with 'aaaaaaaaaaaaaaaaaaaaaa>' after 03.01043
//    Maximum timeout interval of 3 seconds exceeded.
//    
//    Processing aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>
//    Timeout with 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>' after 03.01018
//    Maximum timeout interval of 3 seconds exceeded.
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Security
Imports System.Text.RegularExpressions
Imports System.Threading 

Module Example
   Const MaxTimeoutInSeconds As Integer = 3
   
   Public Sub Main()
      Dim pattern As String = "(a+)+$"    ' DO NOT REUSE THIS PATTERN.
      Dim rgx As New Regex(pattern, RegexOptions.IgnoreCase, TimeSpan.FromSeconds(1))       
      Dim sw As Stopwatch = Nothing
      
      Dim inputs() As String = { "aa", "aaaa>", 
                                 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
                                 "aaaaaaaaaaaaaaaaaaaaaa>",
                                 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>" }
                                 
      For Each inputValue In inputs
         Console.WriteLine("Processing {0}", inputValue)
         Dim timedOut As Boolean = False
         Do 
            Try
               sw = Stopwatch.StartNew()
               ' Display the result.
               If rgx.IsMatch(inputValue) Then
                  sw.Stop()
                  Console.WriteLine("Valid: '{0}' ({1:ss\.fffffff} seconds)", 
                                    inputValue, sw.Elapsed) 
               Else
                  sw.Stop()
                  Console.WriteLine("'{0}' is not a valid string. ({1:ss\.fffff} seconds)", 
                                    inputValue, sw.Elapsed)
               End If
            Catch e As RegexMatchTimeoutException   
               sw.Stop()
               ' Display the elapsed time until the exception.
               Console.WriteLine("Timeout with '{0}' after {1:ss\.fffff}", 
                                 inputValue, sw.Elapsed)
               Thread.Sleep(1500)       ' Pause for 1.5 seconds.

               ' Increase the timeout interval and retry.
               Dim timeout As TimeSpan = e.MatchTimeout.Add(TimeSpan.FromSeconds(1))
               If timeout.TotalSeconds > MaxTimeoutInSeconds Then
                  Console.WriteLine("Maximum timeout interval of {0} seconds exceeded.",
                                    MaxTimeoutInSeconds)
                  timedOut = False
               Else                
                  Console.WriteLine("Changing the timeout interval to {0}", 
                                    timeout) 
                  rgx = New Regex(pattern, RegexOptions.IgnoreCase, timeout)
                  timedOut = True
               End If
            End Try
         Loop While timedOut
         Console.WriteLine()
      Next   
   End Sub 
End Module
' The example displays output like the following:
'    Processing aa
'    Valid: 'aa' (00.0000779 seconds)
'    
'    Processing aaaa>
'    'aaaa>' is not a valid string. (00.00005 seconds)
'    
'    Processing aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
'    Valid: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' (00.0000043 seconds)
'    
'    Processing aaaaaaaaaaaaaaaaaaaaaa>
'    Timeout with 'aaaaaaaaaaaaaaaaaaaaaa>' after 01.00469
'    Changing the timeout interval to 00:00:02
'    Timeout with 'aaaaaaaaaaaaaaaaaaaaaa>' after 02.01202
'    Changing the timeout interval to 00:00:03
'    Timeout with 'aaaaaaaaaaaaaaaaaaaaaa>' after 03.01043
'    Maximum timeout interval of 3 seconds exceeded.
'    
'    Processing aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>
'    Timeout with 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa>' after 03.01018
'    Maximum timeout interval of 3 seconds exceeded.

備註

pattern 參數包含規則運算式語言項目,以透過符號描述要比對的字串。The pattern parameter consists of regular expression language elements that symbolically describe the string to match. 如需正則運算式的詳細資訊, 請參閱.NET Framework 正則運算式正則運算式語言-快速參考主題。For more information about regular expressions, see the .NET Framework Regular Expressions and Regular Expression Language - Quick Reference topics.

Regex物件是不可變的, 這表示它只能用於您在建立時所定義的 match 模式。A Regex object is immutable, which means that it can be used only for the match pattern that you define when you create it. 不過, 不需要重新編譯, 就可以使用任何次數。However, it can be used any number of times without being recompiled.

matchTimeout參數會指定模式比對方法應該在超時之前嘗試尋找相符的時間長度。如果在該時間間隔內找不到相符的, 則模式比對方法RegexMatchTimeoutException會擲回例外狀況。The matchTimeout parameter specifies how long a pattern-matching method should try to find a match before it times out. If no match is found in that time interval, the pattern-matching method throws a RegexMatchTimeoutException exception. matchTimeout針對建立Regex物件的應用程式域, 覆寫任何定義的預設超時值。matchTimeout overrides any default time-out value defined for the application domain in which the Regex object is created. 觀察matchTimeout逾時間隔的實例模式比對方法包括下列各項:The instance pattern-matching methods that observe the matchTimeout time-out interval include the following:

設定逾時間隔, 可防止依賴過量回溯的正則運算式在處理包含接近相符專案的輸入時停止回應。Setting a time-out interval prevents regular expressions that rely on excessive backtracking from appearing to stop responding when they process input that contains near matches. 如需詳細資訊, 請參閱正則運算式和回溯的最佳做法For more information, see Best Practices for Regular Expressions and Backtracking. 若要設定合理的逾時間隔, 請考慮下列因素:To set a reasonable time-out interval, consider the following factors:

  • 正則運算式模式的長度和複雜度。The length and complexity of the regular expression pattern. 較長和較複雜的正則運算式所需的時間比較短、更簡單。Longer and more complex regular expressions require more time than shorter and simpler ones.

  • 預期的電腦負載。The expected machine load. 在具有高 CPU 和記憶體使用率的系統上, 處理會花費更多時間。Processing takes more time on systems that have high CPU and memory utilization.

給呼叫者的注意事項

我們建議您將matchTimeout參數設定為適當的值, 例如兩秒。We recommend that you set the matchTimeout parameter to an appropriate value, such as two seconds. 如果您藉由指定InfiniteMatchTimeout來停用超時, 則正則運算式引擎可提供稍微較佳的效能。If you disable time-outs by specifying InfiniteMatchTimeout, the regular expression engine offers slightly better performance. 不過, 只有在下列情況下, 您才應該停用超時:However, you should disable time-outs only under the following conditions: -當正則運算式所處理的輸入衍生自已知且受信任的來源, 或包含靜態文字時。- When the input processed by a regular expression is derived from a known and trusted source or consists of static text. 這會排除使用者動態輸入的文字。This excludes text that has been dynamically input by users.

-當正則運算式模式經過徹底測試, 以確保它能有效率地處理相符、不相符和接近的相符專案。- When the regular expression pattern has been thoroughly tested to ensure that it efficiently handles matches, non-matches, and near matches.

-當正則運算式模式未包含任何已知在處理接近相符專案時造成過多回溯的語言元素時。- When the regular expression pattern contains no language elements that are known to cause excessive backtracking when processing a near match.

另請參閱

適用於