<p>I am consuming the Twitter API and want to convert all URLs to hyperlinks. </p>

<p>What is the most effective way you've come up with to do this?</p>

<p>from</p>

<pre><code>string myString = "This is my tweet check it out http://tinyurl.com/blah";
</code></pre>

<p>to</p>

<pre><code>This is my tweet check it out &lt;a href="http://tinyurl.com/blah"&gt;http://tinyurl.com/&gt;blah&lt;/a&gt;
</code></pre>

[{'answer_id': 32648, 'author': 'samjudson', 'author_id': 1908, 'author_profile': 'https://Stackoverflow.com/users/1908', 'pm_score': 6, 'selected': True, 'text': '<p>Regular expressions are probably your friend for this kind of task:</p>\n\n<pre><code>Regex r = new Regex(@"(https?://[^\\s]+)");\nmyString = r.Replace(myString, "&lt;a href=\\"$1\\"&gt;$1&lt;/a&gt;");\n</code></pre>\n\n<p>The regular expression for matching URLs might need a bit of work.</p>\n'}, {'answer_id': 32665, 'author': 'Derek Park', 'author_id': 872, 'author_profile': 'https://Stackoverflow.com/users/872', 'pm_score': 3, 'selected': False, 'text': '<p>This is actually an ugly problem.  URLs can contain (and end with) punctuation, so it can be difficult to determine where a URL actually ends, when it\'s embedded in normal text.  For example:</p>\n\n<pre><code>http://example.com/.\n</code></pre>\n\n<p>is a valid URL, but it could just as easily be the end of a sentence:</p>\n\n<pre><code>I buy all my witty T-shirts from http://example.com/.\n</code></pre>\n\n<p>You can\'t simply parse until a space is found, because then you\'ll keep the period as part of the URL.  You also can\'t simply parse until a period or a space is found, because periods are extremely common in URLs.</p>\n\n<p>Yes, regex is your friend here, but constructing the appropriate regex is the hard part.</p>\n\n<p>Check out this as well: <a href="http://www.west-wind.com/WebLog/posts/9779.aspx" rel="noreferrer">Expanding URLs with Regex in .NET</a>.</p>\n'}, {'answer_id': 32693, 'author': 'RedWolves', 'author_id': 648, 'author_profile': 'https://Stackoverflow.com/users/648', 'pm_score': 3, 'selected': False, 'text': '<p>I did this exact same thing with <a href="http://ralphwhitbeck.com/2007/11/20/PullingTwitterUpdatesWithJSONAndJQuery.aspx" rel="nofollow noreferrer">jquery consuming the JSON API</a> here is the linkify function:</p>\n\n<pre><code>String.prototype.linkify = function() {\n    return this.replace(/[A-Za-z]+:\\/\\/[A-Za-z0-9-_]+\\.[A-Za-z0-9-_:%&amp;\\?\\/.=]+/, function(m) {\n        return m.link(m);\n    });\n };\n</code></pre>\n'}, {'answer_id': 2661465, 'author': 'winfred', 'author_id': 319042, 'author_profile': 'https://Stackoverflow.com/users/319042', 'pm_score': 1, 'selected': False, 'text': '<p>/cheer for RedWolves</p>\n\n<blockquote>\n  <p>from: this.replace(/[A-Za-z]+://[A-Za-z0-9-<em>]+.[A-Za-z0-9-</em>:%&amp;\\?/.=]+/, function(m){...</p>\n  \n  <p>see: /[A-Za-z]+://[A-Za-z0-9-<em>]+.[A-Za-z0-9-</em>:%&amp;\\?/.=]+/</p>\n</blockquote>\n\n<p>There\'s the code for the addresses "anyprotocol"://"anysubdomain/domain"."anydomainextension and address", </p>\n\n<p>and it\'s a perfect example for other uses of string manipulation. you can slice and dice at will with .replace and insert proper "a href"s where needed.</p>\n\n<p>I used jQuery to change the attributes of these links to "target=_blank" easily in my content-loading logic even though the .link method doesn\'t let you customize them.</p>\n\n<p>I personally love tacking on a custom method to the string object for on the fly string-filtering (the String.prototype.linkify declaration), but I\'m not sure how that would play out in a large-scale environment where you\'d have to organize 10+ custom linkify-like functions. I think you\'d definitely have to do something else with your code structure at that point.</p>\n\n<p>Maybe a vet will stumble along here and enlighten us.</p>\n'}, {'answer_id': 9139800, 'author': 'herry', 'author_id': 932213, 'author_profile': 'https://Stackoverflow.com/users/932213', 'pm_score': 1, 'selected': False, 'text': '<p>You can add some more control on this by using MatchEvaluator delegate function with regular expression:\nsuppose i have this string:</p>\n\n<pre>find more on http://www.stackoverflow.com </pre>\n\n<p>now try this code</p>\n\n<pre><code>private void ModifyString()\n{\n    string input = "find more on http://www.authorcode.com ";\n                Regex regx = new Regex(@"\\b((http|https|ftp|mailto)://)?(www.)+[\\w-]+(/[\\w- ./?%&amp;=]*)?");\n                string result = regx.Replace(input, new MatchEvaluator(ReplaceURl));\n}\n\nstatic string ReplaceURl(Match m)\n{\n    string x = m.ToString();\n    x = "&lt; a href=\\"" + x + "\\"&gt;" + x + "&lt;/a&gt;";\n    return x;\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/32637', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2347826/']