RegexCompilationInfo コンストラクター

定義

RegexCompilationInfo クラスの新しいインスタンスを初期化します。

オーバーロード

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

アセンブリに含める正規表現についての情報を格納する RegexCompilationInfo クラスの新しいインスタンスを初期化します。

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

アセンブリに含めるタイムアウト値を指定した正規表現についての情報を格納する RegexCompilationInfo クラスの新しいインスタンスを初期化します。

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

アセンブリに含める正規表現についての情報を格納する 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 です。

次の例では、2 つの手順でコンパイルされた正規表現を作成して使用します。

最初の手順で、次のコード例をコンパイルして実行します。 コード例のコンストラクターは 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

2 番目の手順では、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 、コンパイルされた正規表現を含むアセンブリを生成します。 したがって、 の値optionsの 1 つとして を指定Compiledしないでください。

trueの場合ispublic、コンパイルされた正規表現クラスにパブリック アクセシビリティが付与されます。 つまり、任意のアセンブリで実行されるコードからインスタンス化できます。 が の場合ispublic、コンパイル済みの正規表現クラスに (C#では) または Friend (Visual Basic では) アクセシビリティが付与internalfalseされます。 つまり、正規表現クラスと同じアセンブリで実行されるコードからのみインスタンス化できます。

注意 (呼び出し元)

このコンストラクターは、作成されるアプリケーション ドメインの既定のタイムアウト値を使用するコンパイル済み正規表現を作成します。 アプリケーション ドメインにタイムアウト値が定義されている場合、コンパイルされた正規表現では 値 InfiniteMatchTimeoutが使用されます。これにより、パターンマッチング操作のタイムアウトが回避されます。コンパイルされた正規表現を作成するために推奨されるコンストラクターは です RegexCompilationInfo(String, RegexOptions, String, String, Boolean, TimeSpan)。これにより、タイムアウト間隔を設定できます。

適用対象

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

アセンブリに含めるタイムアウト値を指定した正規表現についての情報を格納する 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 が負の値か、0 か、または約 24 日を超えています。

次の例では、 という名前 DuplicateChars の 1 つのコンパイル済み正規表現を定義し、入力文字列内で同じ文字が 2 回以上出現することを識別します。 コンパイルされた正規表現の既定のタイムアウトは 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+ キャプチャされた最初のグループの値の 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 、コンパイルされた正規表現を含むアセンブリを生成します。 したがって、 の値optionsの 1 つとして を指定Compiledしないでください。

trueの場合ispublic、コンパイルされた正規表現クラスにパブリック アクセシビリティが付与されます。 つまり、任意のアセンブリで実行されるコードからインスタンス化できます。 が の場合ispublic、コンパイル済みの正規表現クラスに (C#では) または Friend (Visual Basic では) アクセシビリティが付与internalfalseされます。 つまり、正規表現クラスと同じアセンブリで実行されるコードからのみインスタンス化できます。

パラメーターは matchTimeout 、コンパイルされた正規表現の既定のタイムアウト間隔を定義します。 この値は、コンパイルされた正規表現オブジェクトが 1 回の一致操作を実行してから操作がタイムアウトし、正規表現エンジンが次回のタイミング チェック中に例外をRegexMatchTimeoutExceptionスローするおおよその時間を表します。 タイムアウト値の詳細については、 プロパティを MatchTimeout 参照してください。

重要

コンパイルされた正規表現の既定のタイムアウト値を常に設定することをお勧めします。 正規表現ライブラリのコンシューマーは、このコンストラクターのオーバーロードに新しいタイムアウト間隔を表す値を TimeSpan 渡すことによって、そのタイムアウト値をオーバーライドできます。

こちらもご覧ください

適用対象