user1757913
user1757913

Reputation: 53

Split and find specific text?

ok so i've made a HTTPWEBREQUEST and i've made the source of the result show in a richtextbox, Now say i have this in the richtextbox

<p>Short URL: <code><a href="http://URL.me/u/eywnp">http://URL.me/u/eywnp</a></code></p>

How would i go about just getting the "http://URL.me/u/eywnp" ive tried split but didnt work, guess i'm doing it wrong?

NOTE the URL will be different everytime

Upvotes: 1

Views: 89

Answers (2)

Konrad Rudolph
Konrad Rudolph

Reputation: 545686

Split isn’t the right tool for the job. It will result in a rather complex piece of code that’s quite brittle (meaning it will break as soon as there’s the slightest change in the input).

For a robust, well-written solution you need to parse the HTML properly. Luckily there exist canned solutions for that: The HtmlAgilityPack library.

Dim doc As New HtmlDocument()
doc.LoadHtml(yourCode)
Dim result = doc.DocumentElement.SelectNodes("//a[@href]")(0)("href")

The only complicated part here is the string "//a[@href]". This is an XPath string. XPath strings are a mini-language that is used to address elements in an HTML or XML document. They are conceptually similar to file paths (like C:\Users\foo\Documents\file.txt) but with a slightly different syntax.

The XPath simply selects all the <a> elements having a href attribute from your document. Then you can grab the first of that collection and retrieve the href attribute’s value.

Upvotes: 4

user1757913
user1757913

Reputation: 53

Thanks for all your help, i did find a solution and i used

 Dim iStartIndex, iEndIndex As Integer
        With RichTextBox1.Text
            iStartIndex = .IndexOf("<p>Short URL: <code><a href=") + 29
            iEndIndex = .IndexOf(""">", iStartIndex)
            Clipboard.SetText(.Substring(iStartIndex, iEndIndex - iStartIndex))

        End With

works perfect so far

Upvotes: 2

Related Questions