目标:截取html中<span class='tpc_contend'><br>和</font></span>中间的内容
 
        private void button1_Click(object sender, EventArgs e)
        {    
            string str = "<br><span class='tpc_contend'><br> <br> &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; <font color='red'>  &lt;悔恨的泪&gt; </font></span>";  //实验用str
            string strRes = "";
            Regex reg =new Regex("(?<=<span class='tpc_contend'><br>).*(?=</font></span>)");
            MatchCollection mc = reg.Matches(str);
            for (int i=1;i<mc.count+1;i++)
            {  
                strRes += reg.Matches(str)[i].ToString(); 
            }
            richTextBox1 .Text =strRes.ToString();
      
好像算法不对  求助正则达人  在线等~~

解决方案 »

  1.   

    简单的改了一下   string str1 = "<br><span class='tpc_contend'><br> <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <font color='red'> &lt;悔恨的泪&gt; </font></span>"; //实验用str            Match m = Regex.Match(str1,"(?<=<span class='tpc_contend'><br>).*(?=</font></span>)");
                string strRes = m.Value;
      

  2.   


     string str1 = "<br><span class='tpc_contend'><br> <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <font color='red'> &lt;悔恨的泪&gt; </font></span>"; //实验用str            Match m = Regex.Match(str1, "(?<=<span\\s*class='tpc_contend'[^>]*><br>).*(?=</font>\\s*</span>)");
                string strRes = m.Value;// <br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <font color='red'> &lt;悔恨的泪&gt; 这个好点,不为空
      

  3.   


            private void button1_Click(object sender, EventArgs e)
            {
               string str = richTextBox1.Text;
               Match m = Regex.Match(str, "(?<=<span\\s*class='tpc_contend'[^>]*><br>).*(?=</font>\\s*</span>)"); 
               string  strRes = m.Value;
                richTextBox2.Text =strRes.ToString();
            }
                    老大 我根据你的改了一下 还是空啊 ~~~为神马
      

  4.   

    你调试一下strRes 这个为空吗?
      

  5.   

    (?<=<span\s*class='tpc_contend'[^>]*><br>)(.*)(?=</font>\s*</span>)
      

  6.   


    "br><span class='tpc_contend'><br> <br> &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp;  &nbsp; \n<font color='red'>  &lt;悔恨的泪&gt; </font></span>\""不为空了 strRes是上面那个
    不知何故返回不到richtextbox里面
      

  7.   

    后台复制不行啊
    那是前台<%= strRes%>,很久没做BS了