URL from HTML string.

Give a html string get only url from given string

For example

Input:
<a href="/url?q=https://www.yahoo.com/&amp;sa=U&amp;ved=0ahUKEwizwPy0yNHSAhXMDpAKHec7DAsQFgh6MA0&amp;usg=AFQjCNEjJILXPMMCNAlz5MN1IIzjpr79tw">

Output:
https://www.yahoo.com/

Solution

using System;
using System.Text.RegularExpressions;

namespace UrlString
{
    class Program
    {
        static void Main()
        {
            var input = "<a href=\"/url?q=https://www.yahoo.com/&amp;sa=U&amp;ved=0ahUKEwizwPy0yNHSAhXMDpAKHec7DAsQFgh6MA0&amp;usg=AFQjCNEjJILXPMMCNAlz5MN1IIzjpr79tw\">";
            var regex = new Regex(@"https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}");
            var output = regex.Match(input).Value;
            Console.WriteLine($"URL: {output}");
            Console.ReadLine();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *