RegEx模式来标记除<img>之外的链接的空锚点

我试图分析Excel单元格中的HTML文本并删除一些部分。 该文本可以包含不同的跨度样式,URL,类。 我想最简单的方法是RegEx。

我有六种types的链接(例如,当然可能有不同的属性和值):

2没有锚和没有<img> (应select)

<a href="/"><span style="color: #000000;"></span></a>
<a href="/"></a>

2没有锚和<img> (不应select)

<a href="/" title=""><span style="color: #000000;"></span><img class="cars"></a>
<a href="/" title=""><img class="cars"></a>

和2锚(不应select)

 <a href="/"><span style="color: #000000;">Cars</span></a> <a href="/">Cars</a> 

什么正则expression式模式,我应该用来标记没有锚点和没有<img>的2个链接?

我已经build立了模式

 <a href=".*">(?!<img ".*">)(?:<\/span>)?<\/a> 

这标志着两种types的链接:

<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 

如果您考虑使用正则expression式的解决scheme那么您可以使用HTMLDocument对象。

您可以在VBE中添加一个引用(Microsoft HTML Object Library)来获取这个库,然后使用早期绑定。 或者,对于我下面的示例代码,只需使用迟绑定:

 Dim objHtml As Object Set objHtml = CreateObject("htmlfile") 

我的例子传递一个string来创buildHTMLDocument ,你需要根据这个接受的答案使用迟绑定。

不pipe怎样,您可以使用HTMLDocument对象的方法和属性来检查DOM – 我已经使用getElementsByTagNameinnerTextinnerHTML获取您感兴趣的两个标签。例如:

 ' 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。 在这种情况下,使用早期绑定方法是有用的,因为您将在HTMLDocument对象和方法等上获得智能感知。

我很欣赏我的评论(关于用正则expression式parsingHTML的经典回答)可能看起来很粗鲁。 然而,这是困难的,往往只是徒劳的演习。

希望这种方法给你另一种select,如果你想沿着这条路。