Wie lege ich einen Timeout-Wert in einem von der Quelle kompilierten Regex fest?
Posted: 13 Jan 2025, 10:28
MS Learn empfiehlt, wann immer möglich quellkompilierte Regex zu verwenden. Es empfiehlt sich außerdem, bei der Verarbeitung nicht vertrauenswürdiger Eingaben einen Timeout-Wert zu verwenden. Soweit ich das beurteilen kann, ist es nicht möglich, einen Timeout-Wert für von der Quelle kompilierte Regex anzugeben. Ich habe den regulären Ausdruck
definiert
und möchten eine E-Mail-Adresse wie in diesem Beispiel mit Match m = SCPattern.Match(input) analysieren. Wie lege ich hier einen Timeout-Wert fest?
Hier ist ein MVP:
definiert
Code: Select all
[GeneratedRegex(@"^[0-9A-Z]([-.\w]*[0-9A-Z])*$", RegexOptions.IgnoreCase)]
private static partial Regex Pattern();
// to prevent instantiation inside the loop
private static readonly Regex SCPattern = Pattern();
Hier ist ein MVP:
Code: Select all
using System.Diagnostics;
using System.Text.RegularExpressions;
public partial class Program
{
[GeneratedRegex(@"^[0-9A-Z]([-.\w]*[0-9A-Z])*$", RegexOptions.IgnoreCase)]
private static partial Regex Pattern();
// to prevent instantiation inside the loop
private static readonly Regex SCPattern = Pattern();
public static void Main()
{
Stopwatch sw;
string[] addresses = { //"AAAAAAAAAAA@contoso.com",
"AAAAAAAAAAAAaaaaaaaaaaaa!@contoso.com" };
// The following regular expression should not actually be used to
// validate an email address.
string pattern = @"^[0-9A-Z]([-.\w]*[0-9A-Z])*$";
string pattern2 = @"^[0-9A-Z]([-.\w]*[0-9A-Z])*$"; // use a different pattern name to avoid caching
string input;
foreach (var address in addresses)
{
string mailBox = address[..address.IndexOf('@')];
int index = 0;
// try with regular regex
Console.WriteLine("regular regex:");
for (int ctr = mailBox.Length - 1; ctr >= 0; ctr--)
{
index++;
input = mailBox.Substring(ctr, index);
sw = Stopwatch.StartNew();
Match m = Regex.Match(input, pattern, RegexOptions.IgnoreCase); //, TimeSpan.FromMilliseconds(100)
sw.Stop();
if (m.Success)
Console.WriteLine("{0,2}. Matched '{1,27}' in {2}",
index, m.Value, sw.Elapsed);
else
Console.WriteLine("{0,2}. Failed '{1,27}' in {2}",
index, input, sw.Elapsed);
}
Console.WriteLine();
// try with precompiled regex
index = 0;
Console.WriteLine("precompiled regex:");
for (int ctr = mailBox.Length - 1; ctr >= 0; ctr--)
{
index++;
input = mailBox.Substring(ctr, index);
sw = Stopwatch.StartNew();
Match m = Regex.Match(input, pattern2, RegexOptions.Compiled | RegexOptions.IgnoreCase); // , TimeSpan.FromMilliseconds(100)
sw.Stop();
if (m.Success)
Console.WriteLine("{0,2}. Matched '{1,27}' in {2}",
index, m.Value, sw.Elapsed);
else
Console.WriteLine("{0,2}. Failed '{1,27}' in {2}",
index, input, sw.Elapsed);
}
Console.WriteLine();
// try with source-compiled regex
index = 0;
Console.WriteLine("with source-compiled regex:");
for (int ctr = mailBox.Length - 1; ctr >= 0; ctr--)
{
index++;
input = mailBox.Substring(ctr, index);
sw = Stopwatch.StartNew();
Match m = SCPattern.Match(input); // does not work: , TimeSpan.FromMilliseconds(100)
sw.Stop();
if (m.Success)
Console.WriteLine("{0,2}. Matched '{1,27}' in {2}",
index, m.Value, sw.Elapsed);
else
Console.WriteLine("{0,2}. Failed '{1,27}' in {2}",
index, input, sw.Elapsed);
}
}
Console.WriteLine("Press any key ...");
Console.ReadKey();
}
}