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;/~\+#])?      (不用定义空格)

 

相关文章:

  • 2021-05-14
  • 2021-09-02
  • 2022-12-23
  • 2021-05-21
  • 2021-06-25
  • 2021-11-17
  • 2021-06-29
  • 2021-11-17
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-04
  • 2022-12-23
  • 2022-12-23
  • 2021-07-29
相关资源
相似解决方案