gpt4 book ai didi

c# - 正则表达式卡在某些记录中

转载 作者:太空狗 更新时间:2023-10-30 00:50:38 26 4
gpt4 key购买 nike

虽然 Regex 给出了大多数文档的结果,但有时 Regex 会卡在某些值上。

我说的是卡住的场景。

  1- collection = Regex.Matches(document, pattern,RegexOptions.Compiled);
2- if (collection.Count > 0) //This Line
{

我调试了解决方案并希望在监 window 口中查看 collection 的值。我看到大多数属性的结果如下。

Function evaluation disabled because a previous function evaluation timed out. You must continue execution to reenable function evaluation.

后来卡在第2行

我可以看到正则表达式存在一些问题,所以它进入了循环。

问题:我对此没有任何异常。有什么方法可以在超时后获得异常,以便我的工具可以继续进行其余工作。

 Regex:      @"""price"">(.|\r|\n)*?pound;(?<data>.*?)</span>"

Part of Document : </span><span>1</span></a></li>\n\t\t\t\t<li>\n\t\t\t\t\t<span class=\"icon icon_floorplan touchsearch-icon touchsearch-icon-floorplan none\">Floorplans: </span><span>0</span></li>\n\t\t\t\t</ul>\n\t\t</div>\n </div>\n\t</div>\n<div class=\"details clearfix\">\n\t\t<div class=\"price-new touchsearch-summary-list-item-price\">\r\n\t<a href=\"/commercial-property-for-sale/property-47109002.html\">POA</a></div>\r\n<p class=\"price\">\r\n\t\t\t<span>POA</span>\r\n\t\t\t\t</p>\r\n\t<h2 class=\"address bedrooms\">\r\n\t<a id=\"standardPropertySummary47109002\"

最佳答案

当 Regex 搜索花费的时间过长时,如何获得异常?

请阅读下面有关设置正则表达式搜索超时的信息。

MSDN: Regex.MatchTimeout Property

The MatchTimeout property defines the approximate maximum time interval for a Regex instance to execute a single matching operation before the operation times out. The regular expression engine throws a RegexMatchTimeoutException exception during its next timing check after the time-out interval has elapsed. This prevents the regular expression engine from processing input strings that require excessive backtracking. For more information, see Backtracking in Regular Expressions and Best Practices for Regular Expressions in the .NET Framework.

    public static void Main()
{
AppDomain domain = AppDomain.CurrentDomain;
// Set a timeout interval of 2 seconds.
domain.SetData("REGEX_DEFAULT_MATCH_TIMEOUT", TimeSpan.FromSeconds(2));
Object timeout = domain.GetData("REGEX_DEFAULT_MATCH_TIMEOUT");
Console.WriteLine("Default regex match timeout: {0}",
timeout == null ? "<null>" : timeout);

Regex rgx = new Regex("[aeiouy]");
Console.WriteLine("Regular expression pattern: {0}", rgx.ToString());
Console.WriteLine("Timeout interval for this regex: {0} seconds",
rgx.MatchTimeout.TotalSeconds);
}

// The example displays the following output:
// Default regex match timeout: 00:00:02
// Regular expression pattern: [aeiouy]
// Timeout interval for this regex: 2 seconds

为什么我的正则表达式卡住了?

首先,尝试优化您的正则表达式,尽可能减少反向引用。 stribizhev 评论说他有所进步,因此向他表示敬意。

Another thing: your regex is actually equivalent to "price">[\s\S]?pound;(?.?) (C# declaration: @"""price"">[\s\S]?pound;(?.?)"). It is much better since there is much less backtracking. – stribizhev Jun 4 at 9:23

其次,如果您遇到特定值的问题,要追踪它们,您可以做的第一件事是每次迭代(匹配)逻辑,而不是用一个 -类轮。

MSDN: Match.NextMatch Method

   public static void Main()
{
string pattern = "a*";
string input = "abaabb";

Match m = Regex.Match(input, pattern);
while (m.Success) {
Console.WriteLine("'{0}' found at index {1}.",
m.Value, m.Index);
m = m.NextMatch();
}
}

要在不使用模式的情况下提高基准性能,通常将 Regex 对象放在静态类中并仅实例化一次,并在实例化时将 RegexOptions.Compiled 添加到 Regex(您已经完成)。 ( Source )

附言。能够故意导致始终可重现的超时(也称为无限循环)可能会很方便。我会在下面分享。

string pattern = @"/[a-zA-Z0-9]+(\[([^]]*(]"")?)+])?$";
string input = "/aaa/bbb/ccc[@x='1' and @y=\"/aaa[name='z'] \"]";

关于c# - 正则表达式卡在某些记录中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30635887/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com