如何为URL地址和Email地址自动加上超链接?就是论坛里常用的那种形式。谢谢!

解决方案 »

  1.   

    用正则表达式实现
    using System.Text.RegularExpressions;
    Regex my;
    //自动检测地址
    my=new Regex(@"^((http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9\./=\?%\-&_~`@[\]\':+!]+)",RegexOptions.IgnoreCase);
    str=my.Replace(str,@"<a target=_blank href=$1>$1</a>");my=new Regex(@"((http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9\./=\?%\-&_~`@[\]\':+!]+)$",RegexOptions.IgnoreCase);
    str=my.Replace(str,@"<a target=_blank href=$1>$1</a>");my=new Regex(@"([^>=""])((http|https|ftp|rtsp|mms):(\/\/|\\\\)[A-Za-z0-9\./=\?%\-&_~`@[\]\':+!]+)",RegexOptions.IgnoreCase);
    str = my.Replace(str,@"$1<a target=_blank href=$2>$2</a>");

    my=new Regex(@"([^(http://|http:\\)])((www|cn)[.](\w)+[.]{1,}(net|com|cn|org|cc)(((\/[\~]*|\\[\~]*)(\w)+)|[.](\w)+)*(((([?](\w)+){1}[=]*))*((\w)+){1}([\&](\w)+[\=](\w)+)*)*)",RegexOptions.IgnoreCase);
    str = my.Replace(str,@"<img align=absmiddle src=image/ico/url2.gif border=0><a target=_blank href=http://$2>$2</a>");
    ------------
    str就是需替换的字符串
      

  2.   

    大哥,我还是不知道怎么用呀。
    我的意思是说比如“请访问www.163.com网站查找相关内容”。从这句话中要找到 www.163.com 这个网址,然后给他加上超链接。能不能实现??谢谢