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

标签 c# regex performance

我需要将内联 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/

相关文章:

c# - 服务器端数据库写入的最佳策略

c# - 将任务按完成顺序排序

python - 正则表达式查找替换电影名称python

performance - 在高流量网站中规范化或非规范化

c# - 是否可以在 LINQ 中表达此代码?

c# - 主机启动时间比预期长 - Azure 函数

regex - 为什么我在 NLTK RegexpTokenizer() 中的正则表达式删除了 "is"和 "to"?

regex - PowerShell -replace 获取两个不同字符之间的字符串

performance - 在 Windows 上选择 Google API(API 级别 17 或任何其他 API 级别)时,Android 模拟器不会加载

javascript - 估计浏览器 JS 引擎速度以有条件地禁用动画