RegexCompilationInfo Konstruktoren

Definition

Initialisiert eine neue Instanz der RegexCompilationInfo-Klasse.

Überlädt

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

Initialisiert eine neue Instanz der RegexCompilationInfo-Klasse, die Informationen zu einem regulären Ausdruck enthält, der in eine Assembly eingeschlossen werden soll.

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

Initialisiert eine neue Instanz der RegexCompilationInfo-Klasse, die Informationen zu einem regulären Ausdruck mit einem angegebenen Timeoutwert enthält, der in eine Assembly eingeschlossen werden soll.

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

Source:
RegexCompilationInfo.cs
Source:
RegexCompilationInfo.cs
Source:
RegexCompilationInfo.cs

Initialisiert eine neue Instanz der RegexCompilationInfo-Klasse, die Informationen zu einem regulären Ausdruck enthält, der in eine Assembly eingeschlossen werden soll.

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)

Parameter

pattern
String

Der reguläre Ausdruck, der kompiliert werden soll.

options
RegexOptions

Die bei der Kompilierung des regulären Ausdrucks zu verwendenden Optionen für reguläre Ausdrücke.

name
String

Der Name des Typs, der den kompilierten regulären Ausdruck darstellt.

fullnamespace
String

Der Namespace, zu dem der neue Typ gehört.

ispublic
Boolean

true, um den kompilierten regulären Ausdruck öffentlich sichtbar zu machen, andernfalls false.

Ausnahmen

pattern ist null.

- oder -

name ist null.

- oder -

fullnamespace ist null.

Beispiele

Im folgenden Beispiel wird ein kompilierter regulärer Ausdruck in zwei Schritten erstellt und verwendet.

Kompilieren Sie im ersten Schritt das folgende Codebeispiel, und führen Sie es aus. Der RegexCompilationInfo Konstruktor im Codebeispiel definiert einen kompilierten regulären Ausdruck. Das Ergebnis der Codeausführung ist eine Assembly namens FishRegex.dll, die einen kompilierten regulären Ausdruckstyp namens FishRegexenthält.

// 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

Kompilieren Sie im zweiten Schritt das folgende Codebeispiel mithilfe eines Verweises auf FishRegex.dll, und führen Sie dann die resultierende ausführbare Datei aus. Die ausführbare Datei stimmt mit einer Zielzeichenfolge unter Verwendung des FishRegex Typs überein und zeigt die Übereinstimmung, die Gruppe, die Erfassungsgruppe und die Indexposition der Übereinstimmungen in der Zielzeichenfolge an.

// 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
'

Hinweise

Jeder Parameter des RegexCompilationInfo(String, RegexOptions, String, String, Boolean) Konstruktors entspricht direkt einer Eigenschaft der RegexCompilationInfo -Klasse. Da alle Eigenschaften lese-/schreibzugriff sind, können ihre Werte auch direkt zugewiesen werden.

Die CompileToAssembly -Methode generiert eine Assembly, die kompilierte reguläre Ausdrücke enthält. Daher sollten Sie nicht als einen der Werte von optionsangebenCompiled.

Wenn ispublic ist true, erhält die kompilierte Klasse für reguläre Ausdrücke öffentliche Barrierefreiheit. Das heißt, es kann aus Code instanziiert werden, der in jeder Assembly ausgeführt wird. Wenn ispublic istfalse, wird der kompilierten Klasse für reguläre Ausdrücke (in C#) oder Friend (in Visual Basic) Barrierefreiheit gewährt internal . Das heißt, sie kann nur aus Code instanziiert werden, der in derselben Assembly wie die Klasse des regulären Ausdrucks ausgeführt wird.

Hinweise für Aufrufer

Dieser Konstruktor erstellt einen kompilierten regulären Ausdruck, der den Standardtimeoutwert der Anwendungsdomäne verwendet, in der er erstellt wird. Wenn ein Timeoutwert für die Anwendungsdomäne definiert ist, verwendet der kompilierte reguläre Ausdruck den Wert InfiniteMatchTimeout, der verhindert, dass ein Musterabgleichsvorgang ein Timeout hat. Der empfohlene Konstruktor zum Erstellen eines kompilierten regulären Ausdrucks ist RegexCompilationInfo(String, RegexOptions, String, String, Boolean, TimeSpan), mit dem Sie das Timeoutintervall festlegen können.

Gilt für:

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

Source:
RegexCompilationInfo.cs
Source:
RegexCompilationInfo.cs
Source:
RegexCompilationInfo.cs

Initialisiert eine neue Instanz der RegexCompilationInfo-Klasse, die Informationen zu einem regulären Ausdruck mit einem angegebenen Timeoutwert enthält, der in eine Assembly eingeschlossen werden soll.

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)

Parameter

pattern
String

Der reguläre Ausdruck, der kompiliert werden soll.

options
RegexOptions

Die bei der Kompilierung des regulären Ausdrucks zu verwendenden Optionen für reguläre Ausdrücke.

name
String

Der Name des Typs, der den kompilierten regulären Ausdruck darstellt.

fullnamespace
String

Der Namespace, zu dem der neue Typ gehört.

ispublic
Boolean

true, um den kompilierten regulären Ausdruck öffentlich sichtbar zu machen, andernfalls false.

matchTimeout
TimeSpan

Das standardmäßige Timeoutintervall für den regulären Ausdruck.

Ausnahmen

pattern ist null.

- oder -

name ist null.

- oder -

fullnamespace ist null.

matchTimeout ist negativ, null oder größer als ca. 24 Tage.

Beispiele

Im folgenden Beispiel wird ein einzelner kompilierter regulärer Ausdruck namens DuplicateChars definiert, der zwei oder mehr Vorkommen desselben Zeichens in einer Eingabezeichenfolge identifiziert. Der kompilierte reguläre Ausdruck weist ein Standardtimeout von 2 Sekunden auf. Wenn Sie das Beispiel ausführen, wird eine Klassenbibliothek namens RegexLib.dll erstellt, die den kompilierten regulären Ausdruck enthält.

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

Das Muster für reguläre Ausdrücke (\w)\1+ wird entsprechend der folgenden Tabelle definiert:

Muster BESCHREIBUNG
(\w) Passen Sie ein beliebiges Wortzeichen an, und weisen Sie es der ersten Erfassungsgruppe zu.
\1+ Entspricht mindestens einem Vorkommen des Werts der ersten erfassten Gruppe.

Im folgenden Beispiel wird der DuplicatedChars reguläre Ausdruck verwendet, um doppelte Zeichen in einem Zeichenfolgenarray zu identifizieren. Wenn der DuplicatedChars Konstruktor aufgerufen wird, wird das Timeoutintervall in 0,5 Sekunden geändert.

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

Hinweise

Jeder Parameter des RegexCompilationInfo(String, RegexOptions, String, String, Boolean) Konstruktors entspricht direkt einer Eigenschaft der RegexCompilationInfo -Klasse. Da alle Eigenschaften lese-/schreibzugriff sind, können ihre Werte auch direkt zugewiesen werden.

Die CompileToAssembly -Methode generiert eine Assembly, die kompilierte reguläre Ausdrücke enthält. Daher sollten Sie nicht als einen der Werte von optionsangebenCompiled.

Wenn ispublic ist true, erhält die kompilierte Klasse für reguläre Ausdrücke öffentliche Barrierefreiheit. Das heißt, es kann aus Code instanziiert werden, der in jeder Assembly ausgeführt wird. Wenn ispublic istfalse, wird der kompilierten Klasse für reguläre Ausdrücke (in C#) oder Friend (in Visual Basic) Barrierefreiheit gewährt internal . Das heißt, sie kann nur aus Code instanziiert werden, der in derselben Assembly wie die Klasse des regulären Ausdrucks ausgeführt wird.

Der matchTimeout Parameter definiert das Standardtimeoutintervall für den kompilierten regulären Ausdruck. Dieser Wert stellt den ungefähren Zeitraum dar, in dem ein kompiliertes Objekt für reguläre Ausdrücke einen einzelnen Abgleichsvorgang ausführt, bevor das Timeout des Vorgangs auftritt und die Engine für reguläre Ausdrücke während der nächsten Zeitsteuerung eine RegexMatchTimeoutException Ausnahme auslöst. Weitere Informationen zum Timeoutwert finden Sie in der MatchTimeout -Eigenschaft.

Wichtig

Es wird empfohlen, immer einen Standardtimeoutwert für einen kompilierten regulären Ausdruck festzulegen. Consumer ihrer Bibliothek für reguläre Ausdrücke können diesen Timeoutwert überschreiben, indem sie einen TimeSpan Wert übergeben, der das neue Timeoutintervall an diese Konstruktorüberladung darstellt.

Weitere Informationen

Gilt für: