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.

调用构造函数等效于使用options参数的Regex(String, RegexOptions)None调用构造函数。 Regex(String)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实例化超时值为1秒的对象。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了, 则该示例会将超时值增大到最大值三秒。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对象是不可变的, 这意味着它只能用于在创建时定义的匹配模式。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.

另请参阅

适用于