这里有一个字符串 “请访问如下链接<a hrdf='http://www.sohu.com'>sohu</a>”我想把<a hrdf='http://www.sohu.com'> </a> 去掉。 只剩下“请访问如下链接 sohu”知道的指教下,谢谢

解决方案 »

  1.   

    Regex re = new Regex(@"<a\s+(?:(?!</a>).)*?>|</a>", RegexOptions.None);
    string result = re.Replace(@"请访问如下链接 <a hrdf='http://www.sohu.com'>sohu </a>","");
    Console.WriteLine(result);
    /*
    请访问如下链接 sohu 
    */
      

  2.   

    using System.Text.RegularExpressions;   //已测试过
         string strContent = "<a hrdf='http://www.sohu.com'>sohu </a>";
            Regex urlregex = new Regex(@"(http:\/\/([\w.]+\/?)\S*)", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled); 
            
            strContent = urlregex.Replace(strContent, "$1");
          
            Response.Write(strContent);     
          
      

  3.   

    非常感谢我还想问楼上一个问题,我用C#代码写的,用string.replace <  和 > 之间的,用递归实现的。因为一个输入字符串里可能有多个 链接请问 我的写法和 用正则来比 效率有多大的区别呢??
    谢谢
      

  4.   

     static void Main(string[] args)
            {
                Regex r = new Regex(@"<a[^>]*>|</a>$");
                string result = r.Replace(@"请访问如下链接 <a hrdf='http://www.sohu.com'>sohu </a>", "");
                Console.WriteLine(result);        }