C#code到linkify的URL字符串中的 [英] C# code to linkify urls in a string

查看:143
本文介绍了C#code到linkify的URL字符串中的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有任何好的C#code(和普通的前pressions),将解析字符串和linkify这可能是在字符串中的任何URL?

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?

推荐答案

这是一个pretty简单的任务,你可以用它acheive <一个href=\"http://msdn.microsoft.com/en-us/library/system.text.regularex$p$pssions.regex.replace.aspx\">Regex和一个现成的去与普通的前pression:

It's a pretty simple task you can acheive it with Regex and a ready-to-go regular expression from:

  • http://regexlib.com/

是这样的:

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\-\._\?\,\'/\\\+&amp;%\$#\=~])*$",
                         "<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:

  • Resolve and shorten URLs in C#

另请参见

  • Regular Expression Workbench at MSDN
  • Converting a URL into a Link in C# Using Regular Expressions
  • Regex to find URL within text and make them as link
  • Regex.Replace Method at MSDN
  • The Problem With URLs by Jeff Atwood
  • Parsing URLs with Regular Expressions and the Regex Object
  • Format URLs in string to HTML Links in C#
  • Automatically hyperlink URL and Email in ASP.NET Pages with C#

这篇关于C#code到linkify的URL字符串中的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆