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?
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.
It's a pretty simple task you can acheive it with Regex and a ready-to-go regular expression from:
Something like:
var html = Regex.Replace(html, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+" + "\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?" + "([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$", "<a href=\"$1\">$1</a>");
You may also be interested not only in creating links but in shortening URLs. Here is a good article on this subject:
See also:
well, after a lot of research on this, and several attempts to fix times when
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+&@#/%=~_()|](?=\))|(?<=(?<wrap>[=~|_#]))\b(https?://|www\.)[-A-Za-z0-9+&@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&@#/%=~_()|](?=\k<wrap>)|\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, "<br />"); // 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("<a href=\"{0}\" title=\"{0}\">{1}</a>", temp.Replace(".", periodReplacement).ToLower(), linkMatches[i].ToString().Replace(".", periodReplacement))); } // Clear out period replacement html = html.Replace(periodReplacement, "."); return html; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With