利用正则表达式 替换字符串中多个 URL

 1         Regex urlregex = new Regex(@"(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?",     // 正则表达式              
 2                                 RegexOptions.IgnoreCase | RegexOptions.Compiled);
 3 
 4 
 5                                 string oldProName = modelProName;
 6                                 string newProName = modelProName;
 7 
 8                                 string strtitle = modelProName;
 9 
10                                 if (urlregex.IsMatch(modelProName))    
11                                 {
12                                     foreach (Match match in urlregex.Matches(modelProName)) //Match对象
13                                     {
14                                         string url = match.Value;
15                                         if (url != match.NextMatch().Value)     //防止重复替换相同的链接
16                                         {
17                                             //  newProName = urlregex.Replace(oldProName, "<a href=\"" + url + "\" target=\"_blank\" style=\"color:blue\">点击打开</a>");
18                                             newProName = newProName.Replace(url, "<a href=\"" + url + "\" target=\"_blank\" style=\"color:blue\">点击打开</a>");   //利用String.Replace(),将获取到的子字符串替换成replacement
19                                         }
20                                     }
21 
22 
23                                 }

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

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