文字列として基本的なアンカーノードがあり、そこからURLとテキストを取得したいと思います。例えば:
<a href="http://MyAwesomeWebsite.com/">Go to MyAwesomeWebsite</a>
2つの文字列が必要です。1つは次の文字列です。
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
また、 IndexOfやSubstringなどのStringが提供するメソッドを使用することもできます。しかし、解析を考えている場合は、それらの多くがHtmlAgilePackのようなライブラリを使用することをお勧めします。
Html Agility Packライブラリは、文字列を解析して、必要な情報を返すことができます。スタックオーバーフローの使用方法に関する質問がたくさんあります。
別の方法では、正規表現を使用して、必要なパターンに一致する部分文字列を検索します。