标签:style blog http io color ar os 使用 sp
网上关于 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 anchor 10 (?:\/[\+~%\/\.\w\-]*) // allow optional /path 11 ?\??(?:[\-\+=&;%@\.\w]*) // allow optional query string starting with ? 12 #?(?:[\.\!\/\\\w]*) // allow optional anchor #anchor 13 )? // 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 #anchor 10 )? # make URL suffix optional 11 )", 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=\"${url}\" target=\"_blank\">${url}</a>"); 20 } 21 }
标签:style blog http io color ar os 使用 sp
原文地址:http://www.cnblogs.com/XingchenStudio/p/4095127.html