【问题标题】:Regexp Meta Description正则表达式元描述
【发布时间】:2018-05-02 08:21:01
【问题描述】:

当我在 https://regex101.com (<meta[^>]*description[^>]([^<]+)>) 上使用这个正则表达式时,它会运行。

Match 1
Full match  265-314 `<meta name="description" content="Stackoverflow">`
Group 1.    265-314 `<meta name="description" content="Stackoverflow">`
Group 2.    289-313 ` content="Stackoverflow"

但是当我在我的页面上使用它时它不会运行。

Function GetFirstMatch(PatternToMatch, StringToSearch)
    Set regEx = New RegExp
    regEx.Pattern = PatternToMatch
    regEx.IgnoreCase = True
    regEx.Global = True
    regEx.MultiLine = True
    Set CurrentMatches = regEx.Execute(StringToSearch)

    GetFirstMatch = ""
    If CurrentMatches.Count >= 1 Then
        Set CurrentMatch = CurrentMatches(0)
        If CurrentMatch.SubMatches.Count >= 1 Then
            GetFirstMatch = CurrentMatch.SubMatches(0)
        End If
    End If
    Set regEx = Nothing
End Function

GetFirstMatch("(<meta[^>]*description[^>]([^<]+)>)",sdatai)

谢谢

【问题讨论】:

  • sdatai 中有什么内容?
  • sdatai=pagehtmlcode
  • 请举一个sdatai的例子。
  • &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt; &lt;HTML&gt; &lt;HEAD&gt; &lt;TITLE&gt;My first HTML document&lt;/TITLE&gt; &lt;meta name="description" content="This is an example of a meta description. This will often show up in search results."&gt; &lt;/HEAD&gt; &lt;BODY&gt; &lt;P&gt;Hello world! &lt;/BODY&gt; &lt;/HTML&gt;

标签: regex vbscript asp-classic regex-negation regexp-like


【解决方案1】:

这是因为您在内容中有“描述”一词。这会导致正则表达式的第一部分 (&lt;meta[^&gt;]*description) 跳过所有内容,直到最后一次出现“描述”。这是应该解决这个问题的正则表达式:

(<meta[\s]+name="description"[^>]([^>]+)>)

【讨论】:

    猜你喜欢
    • 2016-09-28
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-15
    相关资源
    最近更新 更多