共用方式為


RegexCompilationInfo 建構函式

定義

初始化 RegexCompilationInfo 類別的新執行個體。

多載

RegexCompilationInfo(String, RegexOptions, String, String, Boolean)

初始化 RegexCompilationInfo 類別的新執行個體,其中包含有關要包含在組件中的規則運算式的資訊。

RegexCompilationInfo(String, RegexOptions, String, String, Boolean, TimeSpan)

初始化 RegexCompilationInfo 類別的新執行個體,這個執行個體包含要在組件中加入指定逾時值之規則運算式的相關資訊。

RegexCompilationInfo(String, RegexOptions, String, String, Boolean)

來源:
RegexCompilationInfo.cs
來源:
RegexCompilationInfo.cs
來源:
RegexCompilationInfo.cs

初始化 RegexCompilationInfo 類別的新執行個體,其中包含有關要包含在組件中的規則運算式的資訊。

public:
 RegexCompilationInfo(System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options, System::String ^ name, System::String ^ fullnamespace, bool ispublic);
public RegexCompilationInfo (string pattern, System.Text.RegularExpressions.RegexOptions options, string name, string fullnamespace, bool ispublic);
new System.Text.RegularExpressions.RegexCompilationInfo : string * System.Text.RegularExpressions.RegexOptions * string * string * bool -> System.Text.RegularExpressions.RegexCompilationInfo
Public Sub New (pattern As String, options As RegexOptions, name As String, fullnamespace As String, ispublic As Boolean)

參數

pattern
String

要編譯的規則運算式。

options
RegexOptions

要在編譯規則運算式時使用的規則運算式選項。

name
String

代表已編譯之規則運算式的型別名稱。

fullnamespace
String

新型別所屬的命名空間。

ispublic
Boolean

如果要使編譯過的規則運算式公開可見,則為 true,否則為 false

例外狀況

nameEmpty

patternnull

-或-

namenull

-或-

fullnamespacenull

範例

下列範例會在兩個步驟中建立和使用編譯的正則運算式。

在第一個步驟中,編譯並執行下列程式碼範例。 程式 RegexCompilationInfo 代碼範例中的建構函式會定義編譯的正則運算式。 執行程式碼的結果是名為 FishRegex.dll 的元件,其中包含名為 FishRegex 的編譯正則運算式類型。

// This code example demonstrates the RegexCompilationInfo constructor
// and the Regex.CompileToAssembly() method.
// compile: csc genFishRegex.cs

namespace MyApp
{
    using System;
    using System.Reflection;
    using System.Text.RegularExpressions;
    class GenFishRegEx
    {
        public static void Main()
        {
// Pattern = Group matches one or more word characters,
//           one or more white space characters,
//           group matches the string "fish".
        string pat = @"(\w+)\s+(fish)";

// Create the compilation information.
// Case-insensitive matching; type name = "FishRegex";
// namespace = "MyApp"; type is public.
        RegexCompilationInfo rci = new RegexCompilationInfo(
                    pat, RegexOptions.IgnoreCase,
                    "FishRegex", "MyApp", true);

// Setup to compile.
        AssemblyName an = new AssemblyName();
        an.Name = "FishRegex";
        RegexCompilationInfo[] rciList = { rci };

// Compile the regular expression.
        Regex.CompileToAssembly(rciList, an);
        }
    }
}
' This code example demonstrates the RegexCompilationInfo constructor
' and the Regex.CompileToAssembly() method.
' compile: csc genFishRegex.cs

Imports System.Reflection
Imports System.Text.RegularExpressions

Class GenFishRegEx
    Public Shared Sub Main() 
        ' Pattern = Group matches one or more word characters, 
        '           one or more white space characters, 
        '           group matches the string "fish".
        Dim pat As String = "(\w+)\s+(fish)"
        
        ' Create the compilation information.
        ' Case-insensitive matching; type name = "FishRegex"; 
        ' namespace = "MyApp"; type is public.
        Dim rci As New RegexCompilationInfo(pat, RegexOptions.IgnoreCase, _
                                            "FishRegex", "MyApp", True)
        
        ' Setup to compile.
        Dim an As New AssemblyName()
        an.Name = "FishRegex"
        Dim rciList As RegexCompilationInfo() = New RegexCompilationInfo() { rci }
        
        ' Compile the regular expression.
        Regex.CompileToAssembly(rciList, an)
    
    End Sub
End Class

在第二個步驟中,使用參考來編譯下列程式碼範例,以FishRegex.dll,然後執行產生的可執行檔。 可執行檔會使用 FishRegex 類型來比對目標字串,並顯示目標字串中相符專案的相符專案、群組、擷取群組和索引位置。

// This code example demonstrates the RegexCompilationInfo constructor.
// Execute this code example after executing genFishRegex.exe.
// compile: csc /r:FishRegex.dll useFishRegex.cs

namespace MyApp
  {
  using System;
  using System.Reflection;
  using System.Text.RegularExpressions;

  class UseFishRegEx
    {
    public static void Main()
      {
// Match against the following target string.
      string targetString = "One fish two fish red fish blue fish";
      int matchCount = 0;
      FishRegex f = new FishRegex();

// Display the target string.
      Console.WriteLine("\nInput string = \"" + targetString + "\"");

// Display each match, capture group, capture, and match position.
      foreach (Match m in f.Matches(targetString))
    {
    Console.WriteLine("\nMatch(" + (++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);
        }
      }
    }
      }
    }
  }

/*
This code example produces the following results:

Input string = "One fish two fish red fish blue fish"

Match(1)
Group(1) = "One"
Capture(0) = "One", Position = 0
Group(2) = "fish"
Capture(0) = "fish", Position = 4

Match(2)
Group(1) = "two"
Capture(0) = "two", Position = 9
Group(2) = "fish"
Capture(0) = "fish", Position = 13

Match(3)
Group(1) = "red"
Capture(0) = "red", Position = 18
Group(2) = "fish"
Capture(0) = "fish", Position = 22

Match(4)
Group(1) = "blue"
Capture(0) = "blue", Position = 27
Group(2) = "fish"
Capture(0) = "fish", Position = 32

*/
' This code example demonstrates the RegexCompilationInfo constructor.
' Execute this code example after executing genFishRegex.exe.
' compile: vbc /r:FishRegex.dll useFishRegex.vb

Imports System.Reflection
Imports System.Text.RegularExpressions

Class UseFishRegEx
    Public Shared Sub Main() 
        ' Match against the following target string.
        Dim targetString As String = "One fish two fish red fish blue fish"
        Dim matchCount As Integer = 0
        Dim f As New MyApp.FishRegex()
        
        ' Display the target string.
        Console.WriteLine(vbLf & "Input string = """ & targetString & """")
        
        ' Display each match, capture group, capture, and match position.
        Dim m As Match
        For Each m In f.Matches(targetString)
            matchCount = matchCount + 1
            Console.WriteLine(vbLf & "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)
                    System.Console.WriteLine("Capture(" & j & ") = """ & c.ToString() & _
                                             """, Position = " & c.Index)
                Next j
            Next i
        Next m
    End Sub
End Class

'
'This code example produces the following results:
'
'Input string = "One fish two fish red fish blue fish"
'
'Match(1)
'Group(1) = "One"
'Capture(0) = "One", Position = 0
'Group(2) = "fish"
'Capture(0) = "fish", Position = 4
'
'Match(2)
'Group(1) = "two"
'Capture(0) = "two", Position = 9
'Group(2) = "fish"
'Capture(0) = "fish", Position = 13
'
'Match(3)
'Group(1) = "red"
'Capture(0) = "red", Position = 18
'Group(2) = "fish"
'Capture(0) = "fish", Position = 22
'
'Match(4)
'Group(1) = "blue"
'Capture(0) = "blue", Position = 27
'Group(2) = "fish"
'Capture(0) = "fish", Position = 32
'

備註

建構函式的每個 RegexCompilationInfo(String, RegexOptions, String, String, Boolean) 參數都會直接對應至 類別的屬性 RegexCompilationInfo 。 因為所有屬性都是可讀寫的,所以也可以直接指派其值。

方法 CompileToAssembly 會產生包含已編譯正則運算式的元件。 因此,您不應該將 指定 Compiled 為 的 options 其中一個值。

如果 ispublictrue ,則編譯的正則運算式類別會提供公用協助工具。 也就是說,它可以從任何元件中執行的程式碼具現化。 如果 ispublicfalse ,則編譯的正則運算式類別會在 C#) 中指定 internal (,或在 Friend Visual Basic) 協助工具中提供 (。 也就是說,它只能從與正則運算式類別相同的元件中執行的程式碼具現化。

給呼叫者的注意事項

這個建構函式會建立編譯的正則運算式,以使用建立它的應用程式域的預設逾時值。 如果為應用程式域定義逾時值,則編譯的正則運算式會使用 值 InfiniteMatchTimeout ,以防止模式比對作業逾時。建立已編譯正則運算式的建議建構函式為 RegexCompilationInfo(String, RegexOptions, String, String, Boolean, TimeSpan) ,可讓您設定逾時間隔。

適用於

RegexCompilationInfo(String, RegexOptions, String, String, Boolean, TimeSpan)

來源:
RegexCompilationInfo.cs
來源:
RegexCompilationInfo.cs
來源:
RegexCompilationInfo.cs

初始化 RegexCompilationInfo 類別的新執行個體,這個執行個體包含要在組件中加入指定逾時值之規則運算式的相關資訊。

public:
 RegexCompilationInfo(System::String ^ pattern, System::Text::RegularExpressions::RegexOptions options, System::String ^ name, System::String ^ fullnamespace, bool ispublic, TimeSpan matchTimeout);
public RegexCompilationInfo (string pattern, System.Text.RegularExpressions.RegexOptions options, string name, string fullnamespace, bool ispublic, TimeSpan matchTimeout);
new System.Text.RegularExpressions.RegexCompilationInfo : string * System.Text.RegularExpressions.RegexOptions * string * string * bool * TimeSpan -> System.Text.RegularExpressions.RegexCompilationInfo
Public Sub New (pattern As String, options As RegexOptions, name As String, fullnamespace As String, ispublic As Boolean, matchTimeout As TimeSpan)

參數

pattern
String

要編譯的規則運算式。

options
RegexOptions

要在編譯規則運算式時使用的規則運算式選項。

name
String

代表已編譯之規則運算式的型別名稱。

fullnamespace
String

新型別所屬的命名空間。

ispublic
Boolean

如果要使編譯過的規則運算式公開可見,則為 true,否則為 false

matchTimeout
TimeSpan

規則運算式的預設逾時間隔。

例外狀況

nameEmpty

patternnull

-或-

namenull

-或-

fullnamespacenull

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

範例

下列範例會定義名為 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

備註

建構函式的每個 RegexCompilationInfo(String, RegexOptions, String, String, Boolean) 參數都會直接對應至 類別的屬性 RegexCompilationInfo 。 因為所有屬性都是可讀寫的,所以也可以直接指派其值。

方法 CompileToAssembly 會產生包含已編譯正則運算式的元件。 因此,您不應該將 指定 Compiled 為 的 options 其中一個值。

如果 ispublictrue ,則編譯的正則運算式類別會提供公用協助工具。 也就是說,它可以從任何元件中執行的程式碼具現化。 如果 ispublicfalse ,則編譯的正則運算式類別會在 C#) 中指定 internal (,或在 Friend Visual Basic) 協助工具中提供 (。 也就是說,它只能從與正則運算式類別相同的元件中執行的程式碼具現化。

參數 matchTimeout 會定義已編譯正則運算式的預設逾時間隔。 這個值代表編譯正則運算式物件會在作業逾時之前執行單一比對作業的大約時間量,而正則運算式引擎會在下次檢查期間擲回 RegexMatchTimeoutException 例外狀況。 如需逾時值的其他資訊,請參閱 MatchTimeout 屬性。

重要

建議您一律為編譯的正則運算式設定預設逾時值。 正則運算式程式庫的取用者可以將代表新逾時間隔的值傳遞 TimeSpan 至這個建構函式多載,以覆寫該逾時值。

另請參閱

適用於