MiscellaneousUser MiscellaneousUser - 16 days ago 6
Vb.net Question

Extract Title from html link

I have the following html string

<a href="/tothepage" title="the page">The Link</a>.


How can i extract title from the HML snippet with ease, either Regex or other. VB.NET solution preferred but C# is ok.

Thanks in advance.

Answer

With a regular expression, the group will contain it ([^"]*):

title="([^"]*)"

C#

using System.Text.RegularExpressions;
static void Main(string[] args)
    {
        string originalString = "<a href=\" / tothepage\" title=\"the page\">The Link</a>.";
        Regex rgx = new Regex("title=\"([^\"]*)\"", RegexOptions.IgnoreCase);
        Match match = rgx.Matches(originalString)[0];
        Console.WriteLine(match.Groups[1]);
        Console.ReadLine();
    }