首页 > 代码库 > [转] Url 转 Link 的 C# 正则表达式

[转] Url 转 Link 的 C# 正则表达式

网上关于 Url 转链接( href )的正则表达式一搜一大堆,但真正好用的没几个。

后来在 Matthew O‘Riordan 的 Blog 上发现一个很好用的正则表达式,是用 Javascript 写的,代码如下:

 1   (  // brackets covering match for protocol (optional) and domain 2     ([A-Za-z]{3,9}:(?:\/\/)?) // match protocol, allow in format http:// or mailto: 3     (?:[\-;:&=\+\$,\w]+@)?    // allow something@ for email addresses 4     [A-Za-z0-9\.\-]+          // anything looking at all like a domain, non-unicode domains 5     |// or instead of above 6     (?:www\.|[\-;:&=\+\$,\w]+@) // starting with something@ or www. 7     [A-Za-z0-9\.\-]+            // anything looking at all like a domain 8   ) 9   ( // brackets covering match for path, query string and anchor10     (?:\/[\+~%\/\.\w\-]*)    // allow optional /path11     ?\??(?:[\-\+=&;%@\.\w]*) // allow optional query string starting with ? 12     #?(?:[\.\!\/\\\w]*)      // allow optional anchor #anchor13   )? // make URL suffix optional

 

针对我们的使用场景(只对 http 或 https 开头的 Url进行转换)简化了一下,并用 C# 写出:

 1 public static class ContentFormatter 2 { 3     private static readonly Regex Url_To_Link = new Regex(@"(?<url> 4         (https?:(?:\/\/)?)        # match protocol, allow in format http:// or https:// 5         [A-Za-z0-9\.\-]+          # anything looking at all like a domain, non-unicode domains         6         (                         # brackets covering match for path, query string and anchor 7         (?:\/[\+~%\/\.\w\-]*)?    # allow optional /path 8         \??(?:[\-\+=&;%@\.\w]*?)  # allow optional query string starting with ?  9         \#?(?:[\.\!\/\\\w\-]*)      # allow optional anchor #anchor10         )?                        # make URL suffix optional11         )",12         RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.IgnorePatternWhitespace,13         TimeSpan.FromMilliseconds(100));14 15     public static string UrlToLink(string text)16     {17         if (string.IsNullOrEmpty(text)) return string.Empty;18 19         return Url_To_Link.Replace(text, "<a href=http://www.mamicode.com/"${url}\" target=\"_blank\">${url}</a>");20     }21 }
 

[转] Url 转 Link 的 C# 正则表达式