<div class="s_txt_jobs">
<table id="txt">
       <tr>
           <td>
           <td>
       </tr>
 </table>
  <table id="txt">
       <tr>
           <td>
           <td>
       </tr>
</table>
</div>求匹配以上HTML的正则表达式,把id="txt"的table源代码取出来,table有可能会有多个id="txt"的。
这正则表达式要怎么写?
谢谢

解决方案 »

  1.   

    取tableMatch m = Regex.Match(str, @"<table[^>]*>(?:(?!<table[^>]*>)[\s\S])*?</table>", RegexOptions.IgnoreCase);
    if (m.Success)
        MessageBox.Show(m.Value);
      

  2.   


                string str = "<div   class= \"s_txt_jobs\">"
                    +"<table id=\"txt\"><tr><td>1<td></tr></table>"
                    +"<table id=\"txt\"><tr><td>2<td></tr></table></div>";
                Regex reg = new Regex(@"<table[^>]*?id=(['""\s]?)txt\1[^>]*?>((?!</table).)*");
                MatchCollection match = reg.Matches(str);
                foreach (Match m in match)
                {
                    Response.Write(m.Value + "<br/>");
                }
    /*
    <table id="txt"><tr><td>1<td></tr>
    <table id="txt"><tr><td>2<td></tr>
    */