gpt4 book ai didi

c# - 使用大文档时运行正则表达式非常慢

转载 作者:行者123 更新时间:2023-11-30 20:14:44 32 4
gpt4 key购买 nike

我需要将内联 css 样式属性转换为其 HTML 标记等价物。我的解决方案有效,但使用 Microsoft .Net Regex 命名空间和长文档(约 40 页 html)运行速度非常慢。我尝试了几种变体,但没有有用的结果。我对执行表达式做了一些总结,但最后调用的只是内置的正则表达式替换方法。

我确定我在滥用正则表达式的贪婪性,但我不确定是否有办法绕过它来使用单个正则表达式实现我想要的。

我希望能够运行以下单元测试:

[Test]
public void TestCleanReplacesFontWeightWithB()
{
string html = "<font style=\"font-weight:bold\">Bold Text</font>";
html = Q4.PrWorkflow.Helper.CleanFormatting(html);
Assert.AreEqual("<b>Bold Text</b>", html);
}
[Test]
public void TestCleanReplacesMultipleAttributesFontWeightWithB()
{
string html = "<font style=\"font-weight:bold; color: blue; \">Bold Text</font>";
html = Q4.PrWorkflow.Helper.CleanFormatting(html);
Assert.AreEqual("<b>Bold Text</b>", html);
}
[Test]
public void TestCleanReplaceAttributesBoldAndUnderlineWithHtml()
{
string html = "<span style=\"font-weight:bold; color: blue; text-decoration: underline; \">Bold Text</span>";
html = Q4.PrWorkflow.Helper.CleanFormatting(html);
Assert.AreEqual("<u><b>Bold Text</b></u>", html);
}
[Test]
public void TestCleanReplaceAttributesBoldUnderlineAndItalicWithHtml()
{
string html = "<span style=\"font-weight:bold; color: blue; font-style: italic; text-decoration: underline; \">Bold Text</span>";
html = Q4.PrWorkflow.Helper.CleanFormatting(html);
Assert.AreEqual("<u><b><i>Bold Text</i></b></u>", html);
}
[Test]
public void TestCleanReplacesFontWeightWithSpaceWithB()
{
string html = "<font size=\"10\" style=\"font-weight: bold\">Bold Text</font>";
html = Q4.PrWorkflow.Helper.CleanFormatting(html);
Assert.AreEqual("<b>Bold Text</b>", html);
}

我用来实现此逻辑的正则表达式有效,但速度非常慢。 C# 代码中的正则表达式如下所示:

public static IReplacePattern IncludeInlineItalicToITag(ICleanUpHtmlFactory factory)
{
return factory.CreateReplacePattern("(<(span|font) .*?style=\".*?font-style:\\s*italic[^>]*>)(.*?)</\\2>", "$1<i>$3</i></$2>");
}
public static IReplacePattern IncludeInlineBoldToBTag(ICleanUpHtmlFactory factory)
{
return factory.CreateReplacePattern("(<(span|font) .*?style=\".*?font-weight:\\s*bold[^>]*>)(.*?)</\\2>", "$1<b>$3</b></$2>");
}
public static IReplacePattern IncludeInlineUnderlineToUTag(ICleanUpHtmlFactory factory)
{
return factory.CreateReplacePattern("(<(span|font) .*?style=\".*?text-decoration:\\s*underline[^>]*>)(.*?)</\\2>", "$1<u>$3</u></$2>");
}

最佳答案

我认为问题是如果它找到 span | font没有定义样式属性的标签,由于 .\*?,它将继续寻找它直到文档结束。 .我没有测试过,但将其更改为 [^>]\*?可能会提高性能。

确保将更改应用于所有 .\*?你有;即使是捕获标签之间内容的那个(在此处使用 [^<]\*?),因为如果文件格式不正确,它将捕获到下一个结束标记。

关于c# - 使用大文档时运行正则表达式非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/163184/

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