有如下文件
<html><head><body>
<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#CCCCCC">
<td><font color="black" size=1>I l<font color="#FF0000">@</font>ve RuBoard</td>
<td valign="top" class="v2" align="right">
<a href="0201794276_ch02lev1sec3.html"><img src="FILES/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
<a href="0201794276_ch03lev1sec1.html"><img src="FILES/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</td></table>
<br>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top"><A NAME="ch03"></A>
<H2 class="docChapterTitle">Chapter 3. Customer Trust</H2>
<blockquote>
<p class="docText"><span class="docEmphasis">Before you trust a man, eat a peck of salt with him.</span></p><p class="docText">桺roverb</p></blockquote>
<blockquote>
<p class="docText"><span class="docEmphasis">Traditional Web development has always set up customers as the "others." They paid the bills but somehow they were a foreign entity intruding on development. Such segregation has consistently backfired. Where developers wanted privacy they got scrutiny; where they wanted blind faith they got distrust. The walls that divide developers from customers sabotage the whole project. XP offers new practices of inclusion that might be hard to swallow at first but pay off immediately.</span></p></blockquote><ul></ul>
</td>
</tr>
</table>
<td></td>
<table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#CCCCCC">
<td><font color="black" size=1>I l<font color="#FF0000">@</font>ve RuBoard</td>
<td valign="top" class="v2" align="right">
          <a href="0201794276_ch02lev1sec3.html"><img src="FILES/previous.gif" width="62" height="15" border="0" align="absmiddle" alt="Previous Section"></a>
          <a href="0201794276_ch03lev1sec1.html"><img src="FILES/next.gif" width="41" height="15" border="0" align="absmiddle" alt="Next Section"></a>
</td></table>
</body></html>用正则表达式找到前后两个<table></table>并将其内容删除,包括<table>标签

解决方案 »

  1.   

    string ReturnValue = "<table width='100%' border='0' cellspacing='0'>OK1</table>Con<table width='100%' cellspacing='0'>OK2</table>";
    string Pattern = @"<table(.*?)</table>";
    RegexOptions _Option = RegexOptions.Singleline ;
    Regex _REG = new Regex(Pattern,_Option ); while(_REG.IsMatch(ReturnValue))
    {
    ReturnValue = _REG.Replace(ReturnValue,"");
    } MessageBox.Show(ReturnValue);
      

  2.   

    楼主, 你是想用正则表达式找到前后两个<table></table>并将其内容删除,包括<table>标签, 那就是想保留中间的那个表的内容?? 对吗?
      

  3.   

    如果是这样, 你可以这样做:
    第一步:
        得到中间的那个表的内容
        SubjectString = ""; // 源字符串
    try {
    Regex RegexObj = new Regex("(?s)<table.*?>((?!table).)*</table>");
    Match MatchResults = RegexObj.Match(SubjectString);
    while (MatchResults.Success) {

    MatchResults = MatchResults.NextMatch();
                    // 这里会循环取得三个表的内容, 你只取第二个表的内容

    } catch (ArgumentException ex) {
    // Syntax error in the regular expression
    }然后用第二个标的内容代替源字符串中<body> ... </body>中间的内容
    代替方法如下:
    string ResultString = null;
    try {
    ResultString = Regex.Replace(SubjectString, "(?s)(?<=<body.*?>).*(?=</body>)", table2);
    } catch (ArgumentException ex) {
    // Syntax error in the regular expression
    }table2及为第二个标的内容
      

  4.   

    string input="那一大段代码";
    string Pattern = @"<table(.*?)</table>";
    Regex reg = new Regex(@"<table.*?</table>",RegexOptions.Singleline);
    string output = reg.Replace(input,"");
      

  5.   

    string input="那一大段代码";
    string Pattern = @"<table(.*?)</table>";//这句去掉
    Regex reg = new Regex(@"<table.*?</table>",RegexOptions.Singleline);
    string output = reg.Replace(input,"");
    这样就可以了:
    string input="那一大段代码";
    Regex reg = new Regex(@"<table.*?</table>",RegexOptions.Singleline);
    string output = reg.Replace(input,"");
      

  6.   

    min_jie(止戈) , 你是不是把人家的三个表都去掉了??
    人家可是要保留第二个表的内容啊
      

  7.   

    提个建议,这种问题先把题目简化一些,只留关键信息就行了.你可以先把每个<Table>内容取出来,只要第2个就行了.
      

  8.   

    string input="那一大段代码";
    Regex reg = new Regex(@"<table.*?</table>(.*?<table.*?</table>.*?)<table.*?</table>",RegexOptions.Singleline);
    string output=reg.Replace(input,"$1");
    这回可以符合楼主的要求了。。
      

  9.   

    min_jie(止戈) 的方法将所有的<table>都去掉了,只剩下
    <html><head><META http-equiv="Content-Type" content="text/html"><!--SafClassName="docChapterTitle"--><!--SafTocEntry="Chapter 2. Project Estimating"--><link rel="STYLESHEET" type="text/css" href="FILES/style.css"><link rel="STYLESHEET" type="text/css" href="FILES/docsafari.css"></head><body></body></html>
      

  10.   

    linuxyf(率人哥哥) 的方法好像可行,但是比较麻烦
    我就是想去掉开始和结束的表中的内容,又没有更简单的方法
      

  11.   

    说的在具体一些
    就是要把文件特定的table去掉这个表的特点是包含next.gif这个字符串,是顶层table,并且里面没有嵌套table
    请高手支招
      

  12.   

    string ResultString = null;
    try {
    ResultString = Regex.Replace(SubjectString, "(?s)<table[\\w|=|\"|%|\\s|#]*? bgcolor=\"#CCCCCC\">((?!table).)*</table>", "");
    } catch (ArgumentException ex) {
    // Syntax error in the regular expression
    }
      

  13.   

    楼主:试试这个,我这里测试通过了。
    string ResultString = null;
    try {
    ResultString = Regex.Replace(SubjectString, "(?s)<table((?!>).)*>((?!table).)*next\\.gif((?!table).)*</table>", "");
    } catch (ArgumentException ex) {
    // Syntax error in the regular expression
    }
      

  14.   

    包含next.gif的那两个表去掉了,楼主,揭帖吧