實(shí)例需求:由網(wǎng)頁(yè)中提取下圖中顏色標(biāo)記部分內(nèi)容,網(wǎng)頁(yè)中其他部分與此三行格式相同。
方法1
Sub Demo()
Dim objRegex As Object
Dim inputString As String
Dim objMatches As Object
Dim objMatch As Object
Set objRegex = CreateObject("VBScript.RegExp")
objRegex.Pattern = "id=[^>]+|[^>]+&#[^<]+"
objRegex.Global = True
inputString = Cells(1,1).Value
Set objMatches = objRegex.Execute(inputString)
For Each objMatch In objMatches
Debug.Print objMatch
Next objMatch
End Sub
【代碼解析】
第6行代碼創(chuàng)建正則對(duì)象。
第7行代碼設(shè)置正則匹配模式。
正則表達(dá)式 | 說(shuō)明 |
---|---|
id=[^>]+ |
匹配以id= 開(kāi)頭,后面跟一個(gè)或者多個(gè)除> 之外的任意字符 |
[^>]+&#[^<]+ |
匹配以一個(gè)或者多個(gè)除> 之外的任意字符開(kāi)頭,后面為&# ,之后為一個(gè)或者多個(gè)除< 之外的任意字符 |
第8行代碼設(shè)置正則全局匹配。
第9~11行代碼將待處理字符串保存在變量中。
第12行代碼執(zhí)行正則匹配。
第13~15行代碼循環(huán)遍歷匹配結(jié)果,第14行代碼輸出到立即窗口,結(jié)果如下圖所示。
方法2
使用常規(guī)正則匹配模式,此正則模式比較容易理解,不再講解其含義。
方法1中靈活使用了字符集[^……]
,限制非某字符,也就實(shí)現(xiàn)了以該字符為邊界的效果,其匹配模式字符串更簡(jiǎn)潔。
Sub Demo2()
Dim objRegex As Object
Dim inputString As String
Dim objMatches As Object
Dim objMatch As Object
Set objRegex = CreateObject("VBScript.RegExp")
objRegex.Pattern = "(id=""[^""]+"").*?<text.*?>(.*?)<\/text>"
objRegex.Global = True
inputString = Cells(1,1).Value
Set objMatches = objRegex.Execute(inputString)
For Each objMatch In objMatches
Debug.Print "ID: " & objMatch.SubMatches(0)
Debug.Print "Text: " & objMatch.SubMatches(1)
Debug.Print "----------------"
Next objMatch
End Sub
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-723644.html
示例數(shù)據(jù)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-723644.html
<g class="node" transform="translate(1980, 861.0625)" id="node-946327"><rect width="120" height="36" transform="translate(-60,-18)" rx="18" ry="18" stroke="darkseagreen" stroke-width="2" style="fill: rgb(255, 255, 255);"></rect><circle r="18" transform="translate(-42,0)" style="fill: url("#image-person-old");"></circle><text transform="translate(18,0)" y="4" text-anchor="middle" style="fill-opacity: 1; font: 12px sans-serif;">Байтайлақ</text><text transform="translate(18,0)" y="10" text-anchor="middle" style="fill-opacity: 1; font: 8px sans-serif;"></text></g>
<g class="node" transform="translate(1980, 907.0625)" id="node-946328"><rect width="120" height="36" transform="translate(-60,-18)" rx="18" ry="18" stroke="darkseagreen" stroke-width="2" style="fill: rgb(255, 255, 255);"></rect><circle r="18" transform="translate(-42,0)" style="fill: url("#image-person-old");"></circle><text transform="translate(18,0)" y="4" text-anchor="middle" style="fill-opacity: 1; font: 12px sans-serif;">Байтоғай</text><text transform="translate(18,0)" y="10" text-anchor="middle" style="fill-opacity: 1; font: 8px sans-serif;"></text></g>
<g class="node" transform="translate(1980, 953.0625)" id="node-946329"><rect width="120" height="36" transform="translate(-60,-18)" rx="18" ry="18" stroke="darkseagreen" stroke-width="2" style="fill: rgb(255, 255, 255);"></rect><circle r="18" transform="translate(-42,0)" style="fill: url("#image-person-old");"></circle><text transform="translate(18,0)" y="4" text-anchor="middle" style="fill-opacity: 1; font: 12px sans-serif;">Оразәлі</text><text transform="translate(18,0)" y="10" text-anchor="middle" style="fill-opacity: 1; font: 8px sans-serif;"></text></g>
到了這里,關(guān)于VBA之正則表達(dá)式(43)-- 從網(wǎng)頁(yè)中提取指定數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!