我正在尝试解析 Excel 单元格中的 html 文本并删除一些部分。该文本可以包含不同的跨度样式、URL、类。我想最简单的方法是 RegEx。
我已经得到了六种类型的链接(例如。当然它们可能有不同的属性和值):
2 没有 anchor 且没有 <img>
(应选)
<a href="/"><span style="color: #000000;"></span></a>
<a href="/"></a>
2 没有 anchor ,有 <img>
(不应该选)
<a href="/" title=""><span style="color: #000000;"></span><img class="cars"></a>
<a href="/" title=""><img class="cars"></a>
和2个带 anchor (不应该选)
<a href="/"><span style="color: #000000;">Cars</span></a>
<a href="/">Cars</a>
我应该使用什么 RegEx 模式来标记没有 anchor 且没有 <img>
的 2 个链接只是?
我已经构建了模式
<a href=".*">(?!<img ".*">)(?:<\/span>)?<\/a>
标记两种类型的链接:
<a href="/" title=""><span style="color: #0000;"></span><img class="cars"></a>
<a href="/" title=""><img class="cars"></a>
包含 <img>
标签。
但如果删除 <img>
中的引号标签:
<a href="/" title=""><img class=cars></a>
它工作正常。
VBA代码:
Public Function txtrpl(ByRef x As String) As String`<br>
With CreateObject("VBScript.RegExp")`<br>
.Global = True`<br>
.Pattern = "<a href="".*"">(?!<img "".*"">)(?:<\/span>)?<\/a>"`<br>
txtrpl= Trim$(.Replace(x, ""))`<br>
End With
End Function
最佳答案
如果您考虑不使用正则表达式的解决方案,那么您可以使用HTMLDocument
目的。
你可以在VBE中添加一个引用(Microsoft HTML Object Library)来获取这个库,然后使用早期绑定(bind)。或者,对于我下面的示例代码,只需使用后期绑定(bind):
Dim objHtml As Object
Set objHtml = CreateObject("htmlfile")
我的示例传递一个字符串来创建 HTMLDocument
,您需要根据 this 使用后期绑定(bind)接受的答案。
无论如何,您可以使用 HTMLDocument
对象的方法和属性来检查 DOM - 我使用了 getElementsByTagName
、innerText
和innerHTML
获取您感兴趣的两个标签。例如:
' we want a tags without anchors and without img
For Each objElement In objElements
' innerText = "" is no anchor
If objElement.innerText = "" Then
' check for <img in innerHtml to avoid a tags with an image
If InStr(1, objElement.innerHtml, "<IMG", vbTextCompare) = 0 Then
Debug.Print objElement.outerHTML
End If
End If
Next objElement
完整示例:
Option Explicit
Sub ParseATags()
Dim strHtml As String
strHtml = ""
strHtml = strHtml & "<html>"
strHtml = strHtml & "<body>"
' 2 without anchors and without <img>
strHtml = strHtml & "<a href=""/""><span style=""color: #000000;""></span></a>"
strHtml = strHtml & "<a href=""/""></a>"
' 2 without anchors and with <img>
strHtml = strHtml & "<a href=""/"" title=""""><span style=""color: #000000;""></span><img class=""cars""></a>"
strHtml = strHtml & "<a href=""/"" title=""""><img class=""cars""></a>"
' and 2 with anchors
strHtml = strHtml & "<a href=""/""><span style=""color: #000000;"">Cars</span></a><br>"
strHtml = strHtml & "<a href=""/"">Cars</a><br>"
strHtml = strHtml & "</body>"
strHtml = strHtml & "</html>"
' must use late binding
' https://stackoverflow.com/questions/9995257/mshtml-createdocumentfromstring-instead-of-createdocumentfromurl
Dim objHtml As Object
Set objHtml = CreateObject("htmlfile")
' add html
With objHtml
.Open
.write strHtml
.Close
End With
' now parse the document
Dim objElements As Object, objElement As Object
' get the <a> tags
Set objElements = objHtml.getElementsByTagName("a")
' we want a tags without anchors and without img
For Each objElement In objElements
' innerText = "" is no anchor
If objElement.innerText = "" Then
' check for <img in innerHtml to avoid a tags with an image
If InStr(1, objElement.innerHtml, "<IMG", vbTextCompare) = 0 Then
Debug.Print objElement.outerHTML
End If
End If
Next objElement
End Sub
您可能正在使用 IE 自动化或其他工具从网页中抓取此 HTML。在这种情况下,使用早期绑定(bind)方法很有用,因为您将获得有关 HTMLDocument 对象和方法等的智能感知。
我很欣赏我的评论(关于使用正则表达式解析 HTML 的非常经典的回答)可能看起来很无礼。然而,它充满了困难,而且常常只是徒劳的练习。
如果您希望走这条路,希望这种方法能为您提供另一种选择。
关于html - 用于标记链接的空 anchor 的 RegEx 模式,其中 <img> 除外,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47108725/