问题:
<link href="http://news.163.com/css/news_weather.css" rel="stylesheet" type="text/css" /><div class="date"><a href="http://news.163.com/weather/" target='_blank'><span class="cDRed">天气预报</span></a> 上海:阴转多云19℃~25℃<img src="http://news.163.com/img/logo/02.gif" border="0" width="18" height="13" align="absmiddle" /> <img src="http://news.163.com/img/logo/01.gif" border="0" width="18" height="13" align="absmiddle" /> </div>要求用正则得出的结果为:
<link href="http://news.163.com/css/news_weather.css" rel="stylesheet" type="text/css" /><div class="date><span class="cDRed">天气预报</span> 上海:阴转多云19℃~25℃<img src="http://news.163.com/img/logo/02.gif" border="0" width="18" height="13" align="absmiddle" /> <img src="http://news.163.com/img/logo/01.gif" border="0" width="18" height="13" align="absmiddle" /> </div>即将<span class="cDRed">天气预报</span>两侧的<a>标签移除掉楼主附言:使用substring + indexOf方法的兄弟勿扰

解决方案 »

  1.   


                string str = "<link href=\"http://news.163.com/css/news_weather.css\" rel=\"stylesheet\" type=\"text/css\" /> <div class=\"date\"> <a href=\"http://news.163.com/weather/\" target='_blank'> <span class=\"cDRed\">天气预报 </span> </a> 上海:阴转多云19℃~25℃ <img src=\"http://news.163.com/img/logo/02.gif\" border=\"0\" width=\"18\" height=\"13\" align=\"absmiddle\" /> <img src=\"http://news.163.com/img/logo/01.gif\" border=\"0\" width=\"18\" height=\"13\" align=\"absmiddle\" /> </div> ";
                Regex re = new Regex(@"(\<a\s[^>]*\>)|(\<\/a\>)");
                textBox1.Text = re.Replace(str,"");
    看似简单的问题,做起来发现并不那么容易。因为没有办法从配对的标签中将里面的文字取出替换,所以只好将全文中所有的A标签给删除了。先发过来,回头再试试有没更好的方法。
      

  2.   

    楼上这样就应该可以   
     Regex re = new Regex(@"(\<a\s[^>]*\>)|(\<\/a\>)");
                textBox1.Text = re.Replace(str,"");
    把<a>...</a>替换为‘’ 
      

  3.   


    string result = Regex.Replace(str, @"<a[^>]*>(?=((?!<span|<a)[\s\S])*?<span[^>]*>\s*天气预报)|(?<=天气预报\s*</span>((?!</span>|</a>)[\s\S])*?)</a>", "", RegexOptions.IgnoreCase);