RegexCompilationInfo.MatchTimeout 속성

정의

정규식의 기본 시간 제한 간격을 가져오거나 설정합니다.

public:
 property TimeSpan MatchTimeout { TimeSpan get(); void set(TimeSpan value); };
public TimeSpan MatchTimeout { get; set; }
member this.MatchTimeout : TimeSpan with get, set
Public Property MatchTimeout As TimeSpan

속성 값

RegexMatchTimeoutException이 throw되기 전에 패턴 일치 작업에서 경과할 수 있는 기본 최대 시간 간격이며, 제한 시간이 비활성화된 경우는 InfiniteMatchTimeout입니다.

예제

다음 예제에서는 입력 문자열에서 동일한 문자가 두 개 이상 발생하는 것을 식별하는 라는 DuplicateChars 단일 컴파일된 정규식을 정의합니다. 컴파일된 정규식의 기본 제한 시간은 2초입니다. 예제를 실행하면 컴파일된 정규식을 포함하는 RegexLib.dll 라는 클래스 라이브러리를 만듭니다.

using System;
using System.Reflection;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
        // Match two or more occurrences of the same character.
        string pattern = @"(\w)\1+";
        
        // Use case-insensitive matching. 
        var rci = new RegexCompilationInfo(pattern, RegexOptions.IgnoreCase,
                                           "DuplicateChars", "CustomRegexes", 
                                           true, TimeSpan.FromSeconds(2));

        // Define an assembly to contain the compiled regular expression.
        var an = new AssemblyName();
        an.Name = "RegexLib";
        RegexCompilationInfo[] rciList = { rci };

        // Compile the regular expression and create the assembly.
        Regex.CompileToAssembly(rciList, an);
   }
}
Imports System.Reflection
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
        ' Match two or more occurrences of the same character.
        Dim pattern As String = "(\w)\1+"
        
        ' Use case-insensitive matching. 
        Dim rci As New RegexCompilationInfo(pattern, RegexOptions.IgnoreCase,
                                            "DuplicateChars", "CustomRegexes", 
                                            True, TimeSpan.FromSeconds(2))

        ' Define an assembly to contain the compiled regular expression.
        Dim an As New AssemblyName()
        an.Name = "RegexLib"
        Dim rciList As RegexCompilationInfo() = New RegexCompilationInfo() { rci }

        ' Compile the regular expression and create the assembly.
        Regex.CompileToAssembly(rciList, an)
   End Sub
End Module

정규식 패턴 (\w)\1+ 는 다음 테이블과 같이 정의됩니다.

무늬 설명
(\w) 단어 문자를 일치시키고 첫 번째 캡처 그룹에 할당합니다.
\1+ 캡처된 첫 번째 그룹의 값이 하나 이상 나타나는지 일치합니다.

다음 예제에서는 정규식을 사용하여 DuplicatedChars 문자열 배열에서 중복 문자를 식별합니다. 생성자를 호출 DuplicatedChars 하면 제한 시간 간격이 .5초로 변경됩니다.

using CustomRegexes;
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      var rgx = new DuplicateChars(TimeSpan.FromSeconds(.5));
      
      string[] values = { "Greeeeeat", "seed", "deed", "beam", 
                          "loop", "Aardvark" };
      // Display regex information.
      Console.WriteLine("Regular Expression Pattern: {0}", rgx);
      Console.WriteLine("Regex timeout value: {0} seconds\n", 
                        rgx.MatchTimeout.TotalSeconds);
      
      // Display matching information.
      foreach (var value in values) {
         Match m = rgx.Match(value);
         if (m.Success)
            Console.WriteLine("'{0}' found in '{1}' at positions {2}-{3}",
                              m.Value, value, m.Index, m.Index + m.Length - 1);
         else
            Console.WriteLine("No match found in '{0}'", value);
      }                                                         
   }
}
// The example displays the following output:
//       Regular Expression Pattern: (\w)\1+
//       Regex timeout value: 0.5 seconds
//       
//       //eeeee// found in //Greeeeeat// at positions 2-6
//       //ee// found in //seed// at positions 1-2
//       //ee// found in //deed// at positions 1-2
//       No match found in //beam//
//       //oo// found in //loop// at positions 1-2
//       //Aa// found in //Aardvark// at positions 0-1
Imports CustomRegexes
Imports System.Text.RegularExpressions

Module Example
   Public Sub Main()
      Dim rgx As New DuplicateChars(TimeSpan.FromSeconds(.5))
      
      Dim values() As String = { "Greeeeeat", "seed", "deed", "beam", 
                                 "loop", "Aardvark" }
      ' Display regex information.
      Console.WriteLine("Regular Expression Pattern: {0}", rgx)
      Console.WriteLine("Regex timeout value: {0} seconds", 
                        rgx.MatchTimeout.TotalSeconds)
      Console.WriteLine()
      
      ' Display matching information.
      For Each value In values
         Dim m As Match = rgx.Match(value)
         If m.Success Then
            Console.WriteLine("'{0}' found in '{1}' at positions {2}-{3}",
                              m.Value, value, m.Index, m.Index + m.Length - 1)
         Else
            Console.WriteLine("No match found in '{0}'", value)
         End If   
      Next                                                         
   End Sub
End Module
' The example displays the following output:
'       Regular Expression Pattern: (\w)\1+
'       Regex timeout value: 0.5 seconds
'       
'       'eeeee' found in 'Greeeeeat' at positions 2-6
'       'ee' found in 'seed' at positions 1-2
'       'ee' found in 'deed' at positions 1-2
'       No match found in 'beam'
'       'oo' found in 'loop' at positions 1-2
'       'Aa' found in 'Aardvark' at positions 0-1

설명

속성 컴파일 MatchTimeout 된 정규식에 대 한 기본 제한 시간 간격을 정의 합니다. 이 값은 컴파일된 정규식이 작업 시간이 초과되고 정규식 엔진이 다음 타이밍 검사 동안 예외를 throw RegexMatchTimeoutException 하기 전에 단일 일치 작업을 실행하는 대략적인 시간을 나타냅니다.

중요

컴파일된 정규식에 대한 기본 제한 시간 값을 항상 설정하는 것이 좋습니다. 정규식 라이브러리의 소비자는 컴파일된 정규식의 클래스 생성자에 새 시간 제한 간격을 나타내는 값을 전달 TimeSpan 하여 해당 시간 제한 값을 재정의할 수 있습니다.

다음과 같은 방법으로 개체에 기본 제한 시간 값을 할당할 RegexCompilationInfo 수 있습니다.

적절한 시간 제한 간격을 설정하려면 다음 요소를 고려합니다.

  • 정규식 패턴의 길이 및 복잡성입니다. 더 길고 복잡한 정규식은 더 짧고 간단한 정규식보다 더 많은 시간이 필요합니다.

  • 예상된 컴퓨터 로드입니다. 처리는 CPU 및 메모리 사용률이 높은 시스템에서 더 많은 시간이 걸립니다.

적용 대상

추가 정보