locked
Check if the link is broken or not and perform a regex replace after. RRS feed

  • Question

  • User-352524747 posted
    @functions {
        public static string CreateLinks(string s)
        {
            // image url
            var imgurl = new Regex(@"((https?):\/\/[^""<\s]+)(?![^<>]*>|[^""]*?<\/a)(?:(?:jpe?g|png|gif))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            s = imgurl.Replace(s, "<img src=\"${0}\" alt=\"imgurl\">");
    
            return s.Replace("\r", "<br>");
        }
    }

    I have this function that replaces a string that is an image link with the image and displays it on the page. I want to check first if the link is broken or not. And if the link is not broken perfom the above function.

    So i looked for something like this:

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.example.com");
    request.Method = WebRequestMethods.Http.Head;
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    bool pageExists = response.StatusCode == HttpStatusCode.OK;

    But the problem is that i have to perform the check for several urls the string may contain. How to make it possible?

    Sunday, April 9, 2017 7:58 PM

Answers

  • User-352524747 posted
            MatchCollection links = Regex.Matches(s, @"((https?):\/\/[^""<\s]+)(?![^<>]*>|[^""]*?<\/a)(?:(?:jpe?g|png|gif))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            foreach (Match link in links)
            {
                if (!IsUrlAvailable(link.Value))
                {
                    s = s.Replace(link.Value, string.Empty);
                }
            }

    Solved. :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 11, 2017 4:05 PM

All replies

  • User-2057865890 posted

    Hi Dow7,

    But the problem is that i have to perform the check for several urls the string may contain. How to make it possible?

    We could use the Regex.Match static method. It is also possible to call Match upon a Regex object. We test the result of Match with the Success property. When true, a Match occurred and we can access its Value or Groups.

    using System;
    using System.Text.RegularExpressions;
    
    class Program
    {
        static void Main()
        {
            // First we see the input string.
            string input = "/content/alternate-1.aspx";
    
            // Here we call Regex.Match.
            Match match = Regex.Match(input, @"content/([A-Za-z0-9\-]+)\.aspx$",
                RegexOptions.IgnoreCase);
    
            // Here we check the Match instance.
            if (match.Success)
            {
                // Finally, we get the Group value and display it.
                string key = match.Groups[1].Value;
                Console.WriteLine(key);
            }
        }
    }

    #Regex.Match Method (String)
    https://msdn.microsoft.com/en-us/library/twcw2f1c(v=vs.110).aspx 

    Best Regards,

    Chris

    Monday, April 10, 2017 5:38 AM
  • User-352524747 posted

    Hi Chris,

    I have so far this code

    MatchCollection matches = Regex.Matches(s, @"((https?):\/\/[^""<\s]+)(?![^<>]*>|[^""]*?<\/a)(?:(?:jpe?g|png|gif))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    
            foreach (Match match in matches)
            {
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(match.Value);
                    request.Method = WebRequestMethods.Http.Head;
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    bool urlvalid = response.StatusCode == HttpStatusCode.OK;
                    if (urlvalid)
                    {
                        s = match.Value.Replace(s, "<img src=\"${0}\" alt=\"imgurl\">");
                    }
    
                }
                catch (WebException ex)
                {
                    s = match.Value;
                }
            }
    

    When i test it with this links, 

    https://media-www-asp.azureedge.net/media/5245130/home-hero-2.png

    https://media-www-asp.azureedge.net/media/5245130/home-hero-000.png

    https://www-asp.azureedge.net/v-2017-03-27-001/images/ui/home-free-courses.png

    i get only the last one.

    What i want to achieve is to show as an image the first and the third link (their status code is OK) and as a text message the second one with the broken link. 

    Monday, April 10, 2017 2:24 PM
  • User-352524747 posted
            MatchCollection links = Regex.Matches(s, @"((https?):\/\/[^""<\s]+)(?![^<>]*>|[^""]*?<\/a)(?:(?:jpe?g|png|gif))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            foreach (Match link in links)
            {
                if (!IsUrlAvailable(link.Value))
                {
                    s = s.Replace(link.Value, string.Empty);
                }
            }

    Solved. :)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 11, 2017 4:05 PM