vb正则表达式字符串匹配

时间:2015-05-04 10:29:54

标签: regex vb.net

使用vge.net使用正则表达式我将如何恢复href和成本?

我尝试了各种选项,并且刚刚了解到正则表达式可能因语言不同而有所不同,这意味着我浪费了2天的时间来试图找出它

<div class="single-album" id="m-1_1184">
<span class="album-time link-text">
<a class="album-link  tag-b b-ltxt-album b-sec-b b-tab-toy"
href="/cx/1.1184"
title="album | 5 cost">13£50</a>
</span>`enter code here`
<span class="separator">|</span>
</div>

1 个答案:

答案 0 :(得分:1)

我真的会反对using regex to parse HTML。而是使用HtmlAgilityPack

然后它简单而安全:

Dim html As String = File.ReadAllText("C:\Temp\html.txt") ' i've used this text file for your input
Dim doc = New HtmlAgilityPack.HtmlDocument()
doc.LoadHtml(html)
Dim aHref As HtmlAgilityPack.HtmlNode = doc.DocumentNode.SelectSingleNode("//a[@class='album-link  tag-b b-ltxt-album b-sec-b b-tab-toy']")
If aHref IsNot Nothing Then
    Dim href As String = aHref.GetAttributeValue("href", "") ' /cx/1.1184
    Dim title As String = aHref.GetAttributeValue("title", "")
    Dim costs As String = title.Split("|"c).Last().Trim()    ' 5 cost
End If