C# code to linkify urls in a string

后端 未结 7 1405
Happy的楠姐
Happy的楠姐 2020-11-28 06:17

Does anyone have any good c# code (and regular expressions) that will parse a string and \"linkify\" any urls that may be in the string?

7条回答
  •  旧时难觅i
    2020-11-28 06:41

    well, after a lot of research on this, and several attempts to fix times when

    1. people enter in http://www.sitename.com and www.sitename.com in the same post
    2. fixes to parenthisis like (http://www.sitename.com) and http://msdn.microsoft.com/en-us/library/aa752574(vs.85).aspx
    3. long urls like: http://www.amazon.com/gp/product/b000ads62g/ref=s9_simz_gw_s3_p74_t1?pf_rd_m=atvpdkikx0der&pf_rd_s=center-2&pf_rd_r=04eezfszazqzs8xfm9yd&pf_rd_t=101&pf_rd_p=470938631&pf_rd_i=507846

    we are now using this HtmlHelper extension... thought I would share and get any comments:

        private static Regex regExHttpLinks = new Regex(@"(?<=\()\b(https?://|www\.)[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|](?=\))|(?<=(?[=~|_#]))\b(https?://|www\.)[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|](?=\k)|\b(https?://|www\.)[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
    
        public static string Format(this HtmlHelper htmlHelper, string html)
        {
            if (string.IsNullOrEmpty(html))
            {
                return html;
            }
    
            html = htmlHelper.Encode(html);
            html = html.Replace(Environment.NewLine, "
    "); // replace periods on numeric values that appear to be valid domain names var periodReplacement = "[[[replace:period]]]"; html = Regex.Replace(html, @"(?<=\d)\.(?=\d)", periodReplacement); // create links for matches var linkMatches = regExHttpLinks.Matches(html); for (int i = 0; i < linkMatches.Count; i++) { var temp = linkMatches[i].ToString(); if (!temp.Contains("://")) { temp = "http://" + temp; } html = html.Replace(linkMatches[i].ToString(), String.Format("{1}", temp.Replace(".", periodReplacement).ToLower(), linkMatches[i].ToString().Replace(".", periodReplacement))); } // Clear out period replacement html = html.Replace(periodReplacement, "."); return html; }

提交回复
热议问题