运用正则表达式识别 文本中的url 并包装进<a>中

 1 //首先 using System.Text.RegularExpressions;
 2 
 3 
 4 
 5 //网址前后为空格(自己定义)
 6         string proName = "请对网站 http://www.baidu.com 进行评价 ";
 7 
 8         string strContent = proName;
 9 
10 //url正则表达式,只能识别http开头 url结尾不定 要定义一个结尾
11         Regex urlregex = new Regex(@"( http:\/\/([\w.]+\/?)\S* )",  //前后空格与 预定义的一致
12         RegexOptions.IgnoreCase | RegexOptions.Compiled);
13 
14         if (urlregex.IsMatch(proName))
15         {
16             string url = urlregex.Match(proName).Value;  //获取匹配字符串
17             strContent = urlregex.Replace(strContent,
18        "<a href=\"" + url + "\" target=\"_blank\">点击打开</a>");
19 
20         }

(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?      (不用定义空格)

本文来自博客园,作者:mushishi,转载请注明原文链接:https://www.cnblogs.com/mushishi/archive/2013/04/28/3048729.html

原文地址:https://www.cnblogs.com/mushishi/p/3048729.html