我有一个基本的锚节点作为字符串,并希望从中获取 URL 和文本。例如:
<a href="http://MyAwesomeWebsite.com/">Go to MyAwesomeWebsite</a>
我想要两个字符串,一个带有:
http://MyAwesomeWebsite.com/
另一个与
MyAwesomeWebsite
我该如何编码?
您可以使用正则表达式来提取您想要的文本:
Imports System.Text.RegularExpressions
Sub Main()
Dim anchor As String
anchor = "<a href=""http://MyAwesomeWebsite.com/"">Go to MyAwesomeWebsite</a>"
Dim href As String = Regex.Match(anchor, "\""[a-z,A-Z,0-9,:,/,.]+\""").Value
Console.WriteLine(href.Substring(1, href.Length - 2))
Dim content As String = Regex.Match(anchor, "\>[a-z,A-Z,0-9,:,/,., ]+\<").Value
Console.WriteLine(content.Substring(1, content.Length - 2))
Console.ReadKey()
End Sub
您也可以使用String提供的方法,例如IndexOf和Substring。但是,如果您正在考虑解析其中的很多想法,我建议您使用像HtmlAgilePack这样的库。
Html Agility Pack库可以为您解析字符串并返回您想要的任何信息。这里有很多关于如何使用stackoverflow的相关问题。
另一种方法涉及使用正则表达式来查找与所需模式匹配的子字符串。