【问题标题】:RegEx - Hangs forever [duplicate]RegEx - 永远挂起[重复]
【发布时间】:2019-09-12 08:02:34
【问题描述】:

我在 .NET 中遇到了 RegEx 替换的奇怪行为。 我拥有的正则表达式是从特定字符串替换 html 标签,只留下<br> 标签。以下是我尝试使用的 HTML。

<h3 class="a-spacing-mini" style="box-sizing: border-box; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; text-size-adjust: 100%; text-rendering: optimizelegibility; font-size: 17px; line-height: 1.255; font-family: Arial, sans-serif; color: rgb(17, 17, 17); background-color: rgb(255, 255, 255); margin-bottom: 6px !important;">Support Healthy Memory &amp; Hormone Balance*</h3><h5 class="a-spacing-mini a-color-secondary" style="box-sizing: border-box; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; text-size-adjust: 100%; font-size: 13px; line-height: 19px; font-family: &quot;Amazon Ember&quot;, Arial, sans-serif; background-color: rgb(255, 255, 255); margin-bottom: 6px !important; color: rgb(136, 136, 136) !important;">Fight pregnenolone decline</h5><p class="a-spacing-base" style="box-sizing: border-box; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; text-size-adjust: 100%; color: rgb(17, 17, 17); font-family: &quot;Amazon Ember&quot;, Arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); margin-bottom: 14px !important;">Pregnenolone is made from cholesterol in the mitochondria of the adrenal glands and nervous system. Since its levels decline with age, supplementation is recommended.</p><h5 class="a-spacing-mini a-color-secondary" style="box-sizing: border-box; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; text-size-adjust: 100%; font-size: 13px; line-height: 19px; font-family: &quot;Amazon Ember&quot;, Arial, sans-serif; background-color: rgb(255, 255, 255); margin-bottom: 6px !important; color: rgb(136, 136, 136) !important;">The 'mother hormone'</h5><p class="a-spacing-base" style="box-sizing: border-box; margin-top: 0px; margin-right: 0px; margin-left: 0px; padding: 0px; text-size-adjust: 100%; color: rgb(17, 17, 17); font-family: &quot;Amazon Ember&quot;, Arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); margin-bottom: 14px

下面是我用来替换所有标签的VB.NET代码,除了&lt;br&gt;

    Public Shared Function StripHTMLTagsAllowBreakOnly(ByVal text As String) As String
        Dim AcceptableTags As String = "br"
        Dim WhiteListPattern As String = "</?(?(?=" & AcceptableTags & ")notag|[a-zA-Z0-9]+)(?:\s[a-zA-Z0-9\-]+=?(?:([""']?).*?\1?)?)*\s*/?>"
        text = Regex.Replace(text, WhiteListPattern, "", RegexOptions.Compiled)
        Return text
    End Function

有趣的是,当这个方法使用上面提到的 HTML 执行时,它永远不会完成对 RegEx.Replace 方法的调用。

有人对此有想法吗?是具体的表达方式还是别的什么。

我知道 HTML 不完整,并且缺少一些结束标签,但是,我从没想过 RegEx 会永远挂在那里。 (在实时服务器上,它挂起超过 24 小时,不得不终止进程)。

谢谢

【问题讨论】:

  • 看起来你可以像"(?&gt;&lt;/?)(?!" &amp; AcceptableTags &amp; "\b)[a-zA-Z0-9]+(?:\s+[a-zA-Z0-9-]+(?:=(?:([""']?).*?\1?)?))*\s*/?&gt;"一样修复它
  • 我会推荐 HtmlAgilityPack 库用于任何涉及解析 HTML 的工作。让 Regex 与解析 HTML 一起工作是痛苦且容易出错的。
  • 有人说Parse HTML with Regex吗?因为我听说有人说 Parse HTML with Regex!

标签: html .net regex


【解决方案1】:

好像和它一起工作

Dim AcceptableTags As String = "br"
Dim WhiteListPattern As String = ("<(?!" + (AcceptableTags + ")[^>]*(>|$)"))
text = Regex.Replace(text, WhiteListPattern, "", RegexOptions.Compiled)

但是,RegEx 不应该进入无限循环(我猜是这样,这就是它永远运行的原因)。

抛出错误或其他东西更有意义,但永远卡在某个地方没有意义。

谢谢大家

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-23
    • 2021-08-29
    相关资源
    最近更新 更多