Regex 我可以只取用户名而不取href链接吗?
今天我遇到了一个大问题,几个小时以来我一直在研究许多不同的方法,但没有一种对我有效 我试过了Regex 我可以只取用户名而不取href链接吗?,regex,vb.net,Regex,Vb.net,今天我遇到了一个大问题,几个小时以来我一直在研究许多不同的方法,但没有一种对我有效 我试过了 介于函数之间 正则表达式 HTML敏捷包 问题是在VB.NET中,我想抓取一部电影的标题,忽略之前的html链接。但问题是我不能,因为每个标题的链接都会改变,我不理解Regex为它创建代码 这里是代码和部分,说电影链接1是我想抓住的 <a href="/download/fast-and-furious-7-2015-hd-ts-xvid-ac3-hq-hive-cm8-t10472303.htm
<a href="/download/fast-and-furious-7-2015-hd-ts-xvid-ac3-hq-hive-cm8-t10472303.html" class="cellMainLink">**Movie Link 1**</a>
当然,我还需要抓取其他标题。所以我得到的代码是这个,它不起作用
Dim r As New System.Text.RegularExpressions.Regex("class=""cellMainLink"">(?<name>.*)</a>")
Dim matches As MatchCollection = r.Matches(rssourcecode)
For Each itemcode As Match In matches
ListBox1.Items.Add(itemcode.Groups(2).Value)
Next
Dim r作为新系统.Text.RegularExpressions.Regex(“class=”“cellMainLink”“>(?*))
将匹配项设置为MatchCollection=r.matches(rssourcecode)
对于每个itemcode,作为匹配项中的匹配项
ListBox1.Items.Add(itemcode.Groups(2.Value)
下一个
任何能帮助我的人请尽快给我回电话
多谢各位 使用您可以使用以下代码:
Dim links As New List(Of String)()
Dim htmlDoc As New HtmlAgilityPack.HtmlDocument()
htmlDoc.LoadHtml(WebSource)
For Each link As HtmlNode In htmlDoc.DocumentNode.SelectNodes("//a[@class]")
Dim att As HtmlAttribute = link.Attributes("class")
If att.Value = "cellMainLink" Then
links.Add(link.Value)
End If
Next
我认为这里不需要正则表达式解决方案。但是,仅出于教育目的:
Dim ptrn As String = "<a\b[^>]*?class=[""']?cellMainLink[""']?[^>]*?>(.*?)</a>"
Dim input As String = "<a href=""/download/fast-and-furious-7-2015-hd-ts-xvid-ac3-hq-hive-cm8-t10472303.html"" class=""cellMainLink"">**Movie Link 1**</a>"
Dim dds As List(Of String) = New List(Of String)
Dim rx As Regex = New Regex(ptrn)
Dim result As String = rx.Match(input).Groups(1).Value
Dim ptrn As String=“”
Dim dds作为列表(字符串)=新列表(字符串)
作为正则表达式的Dim rx=新正则表达式(ptrn)
Dim结果为字符串=rx.Match(输入).Groups(1).Value
结果:
**Movie Link 1**
。我从你的标题中删除了标记:我正在查看你的HTML Agility pack代码,但我不知道如何将其实现到我的代码中。regex呢?如果您的
标记值跨越多行,您可以添加(?s)
内联选项:将ptrn设置为String=“(?s)]*?class=[“”]?cellMainLink[“”]?[^>]*?>(?)
。要么我获取此数据的方式错误,要么您被误解了。抱歉,我不是故意说得粗鲁,只是说我不知道如何实现该代码你是说你不知道如何使用Regex
对象吗?只需在文件顶部添加Imports System.Text.RegularExpressions
。对于Agility Pack,您需要将其作为NuGet软件包安装(右键单击解决方案名称并选择Manage NuGetPackages,然后搜索并安装。请参阅)我知道如何安装regex和Agility Pack,但当我使用web客户端下载站点和regex来刮取代码时,它不起作用。