背景:
  1.有域名aa.com,域名bb.com;
  2.另有一段文本内容,其中可能有包含"aa.com"或者"bb.com"(也可能是二级域名)的链接或者地址。  需求:
  如果文本中包含aa.com,bb.com的字符串是链接,则不处理;如果不是链接,将此url加上新页面打开的链接。  例子:
  文本1为:请登录www.a.aa.com/xxx.htm ,将www.a.aa.com/xxx.htm 加上链接。则处理为<a href='www.aa.com/xxx.htm' target='_blank'>www.a.aa.com/xxx.htm</a>
  文本2为:请登录<a href='http://www.a.aa.com/xxx.htm'>xxx</a> 则跳过不处理 

解决方案 »

  1.   

    ..判断有没有<a href    这个。  有就不处理。   其他的就看你怎么写- - 
      

  2.   

    如果地址是www.cc.com?para=www.aa.com 的情况是不行的。感觉挺复杂的啊。
      

  3.   

    yourhtml=Regex.Replace(yourhtml,@"(?i)(?<!<a\s+href=(['""])?.*?)(?:https?://)?www\.a\.(?:aa|bb)\.com[^'""\s]+","<a href='$0' target='_blank'>$0</a>");
    "
      

  4.   

    貌似有些小问题额。。string source = "www.a.aa.com/xxx.htm<a href='http://www.a.aa.com/xxx.htm'>xxx</a>www.b.bb.com/xxx.htm";
            source = Regex.Replace(source, @"(?i)(?<!<a\s+href=(['""])?.*?)(?:https?://)?www\.a\.(?:aa|bb)\.com[^'""\s]+", "<a href='$0' target='_blank'>$0</a>");
            Response.Write(source);
      

  5.   

    (?i)(?<!<a\s+href=(['"])?(?:https?://)?)www\.[ab]\.(?:aa|bb)\.com[^'"<\s]+
      

  6.   

    多谢了,一般情况下是OK了。但是在“www.c.com?site=www.a.aa.com略去200字”类似参数里面有关键字的情况下还有些小问题。
    另外求大神推荐一些学习正则的书或者链接吧。感觉作为一个伸手党很惭愧。
      

  7.   

    ...大哥- - 一个url地址栏 你丢那么多东西   还要去截取他。  
    太麻烦了点吧。   
      

  8.   

    同学,看你根深苗正,是个练武的奇材
    我这有本武林秘籍,就送你吧:   http://download.csdn.net/detail/q107770540/2844387
      

  9.   

    哥,还在吗,我把你的正则改了改,貌似原先是链接的也被我加上链接了。看了半天没发现问题,能帮忙看下嘛。protected void Page_Load(object sender, EventArgs e)
        {
            string domainReg = string.Empty;
            string[] domains = { "aa.com", "bb.com", "tmall.com" };
            foreach (string domain in domains)
            {
                domainReg += domain + "|";
            }
            if (!string.IsNullOrEmpty(domainReg))
            {
                domainReg = domainReg.TrimEnd('|');
            }        string source = "www.c.com 略去200字 <a href='http://www.aa.com/xxx.htm'>xxx</a> 123 www.aaaa.aa.com/xxx.aspx 123123<br />123123123www.aa.com<br />http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=";
            string source2 = Regex.Replace(source, string.Format(@"(?is)(?<!<a\b\s+href=(['""]))((?:http(s)?://)|(?:www\.))(\w+\.)?({0})([^'""<\s]+)?", domainReg), "<a href='http://$0' target='_blank'>$0</a>");        Response.Write("加链接前:" + source);
            Response.Write("<br />");
            Response.Write("加链接后:" + source2);
        }
      

  10.   


    void Main()
    {

    string[] domains = { "aa.com", "bb.com", "tmall.com" };
    string domainReg = string.Join("|",domains);
     
    string pattern=@"(?i)(?<!<a\s+href=(['""])?(?:https?://)?)(?:https?://)?www\.(\w+\.)?(?:{0})[^'""<\s]*";
     
    string source = "www.c.com 略去200字 <a href='http://www.aa.com/xxx.htm'>xxx</a> 123 www.aaaa.aa.com/xxx.aspx 123123<br />123123123www.aa.com<br />http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=";
    string source2 = Regex.Replace(source, string.Format(pattern,domainReg),"<a href='http://$0' target='_blank'>$0</a>"); Response.Write("加链接前:" + source);
    Response.Write("<br />");
    Response.Write("加链接后:" + source2);
           /*
       加链接前:www.c.com 略去200字 <a href='http://www.aa.com/xxx.htm'>xxx</a> 123 www.aaaa.aa.com/xxx.aspx 123123<br />123123123www.aa.com<br />http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=
     
       加链接后:www.c.com 略去200字 <a href='http://www.aa.com/xxx.htm'>xxx</a> 123 <a href='http://www.aaaa.aa.com/xxx.aspx' target='_blank'>www.aaaa.aa.com/xxx.aspx</a> 123123<br />123123123<a href='http://www.aa.com' target='_blank'>www.aa.com</a><br /><a href='http://http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=' target='_blank'>http://www.tmall.com/go/act/sale/pptmzz.php?spm=1.1000386.222017.12&scm=1005.21.1.2107&ad_id=&am_id=1301016103dffcaafe71&cm_id=&pm_id=</a>
       */
      

  11.   

    来个终极版本的
    @"(?i)(?<!<a\s+href=['""]?[^'""<\s,;|]*)((?:((https:|http:)?//))?|(www\.)?)(\w+\.)?(?:{0})[^'""<\s,;|\uFF00-\uFFFF\u4e00-\u9fa5]*"