<table id="tbcontent" class="class1" bordercolor="#C0DE98" border="1">
                        <tr>
                            <th>
                               列头1
                            </th>
                            <th>
                               列头2
                            </th>
                            <th>
                                列头3
                            </th>
                            <th>
                                列头4
                            </th>
                            <th>
                                列头5
                            </th>
 
                        </tr>
                    <tr>
                        <td>
                            值1
                        </td>
                        <td>
                            值1
                        </td>
                        <td>
                           值1
                        </td>
                        <td>
                           值1
                        </td>
                        <td>
                            值1
                        </td>  
                    </tr>
                <tr>  <td>
                            值1
                        </td>
                        <td>
                            值1
                        </td>
                        <td>
                           值1
                        </td>
                        <td>
                           值1
                        </td>
                        <td>
                            值1
                        </td>
</tr>
                    </table>现在想求一个正则表达式,上面的<th>是列头,下面的是值,如何把列头取出来,然后把值对应到列里面,呵呵,谢谢了

解决方案 »

  1.   

    (?<=<(\w+).*?>).*(?=<\/\1>)
      

  2.   

            string pstr = @"<t(h|d)>(?<c1>[^<]*)</t\1>\s*<t\1>(?<c2>[^<]*)</t\1>\s*<t\1>(?<c3>[^<]*)</t\1>\s*<t\1>(?<c4>[^<]*)</t\1>\s*<t\1>(?<c5>[^<]*)</t\1>";        MatchCollection mc = Regex.Matches(this.TextBox1.Text, pstr, RegexOptions.Singleline);
            for (int i = 0; i < mc.Count; i++)
            {
                Response.Write("c1:" + mc[i].Groups["c1"].Value + "c2:" + mc[i].Groups["c2"].Value + "c3:" + mc[i].Groups["c3"].Value + "c4:" + mc[i].Groups["c4"].Value + "c5:" + mc[i].Groups["c5"].Value + "<br>");
            }
      

  3.   

    1.确定有多少列
    string pstr = "<th>.*?</th>";
    int count = Regex.Matches(str, pstr, RegexOptions.Singleline).Count;2.拼接正则pstr = @"<t(h|d)>(?<c1>[^<]*)</t\1>";
    for(int i=2;i<=count;i++)
    {
    pstr += @"\s*<t\1>(?<c" + i.ToString() + @">[^<]*)</t\1>";
    }3.匹配        MatchCollection mc = Regex.Matches(str, pstr, RegexOptions.Singleline);
            for (int i = 0; i < mc.Count; i++)
            {
    for(int i=1;i<=count;i++)
    {
    Response.Write("c"+i.ToString() + ":" + mc[i].Groups["c"+i.ToString()].Value);
    }
    }
      

  4.   

    如果你的数据都没漏掉的,可以这样。
    我没判断是否出现某行不足的情况。        private static void TestRegex26()
            {
                string html = @"<table id=""tbcontent"" class=""class1"" bordercolor=""#C0DE98"" border=""1"">
      <tr>
      <th>
      列头1
      </th>
      <th>
      列头2
      </th>
      <th>
      列头3
      </th>
      <th>
      列头4
      </th>
      <th>
      列头5
      </th>
      
      </tr>
      <tr>
      <td>
      值1
      </td>
      <td>
      值1
      </td>
      <td>
      值1
      </td>
      <td>
      值1
      </td>
      <td>
      值1
      </td>   
      </tr>
      <tr> <td>
      值1
      </td>
      <td>
      值1
      </td>
      <td>
      值1
      </td>
      <td>
      值1
      </td>
      <td>
      值1
      </td>
    </tr>
      </table>";
                Match m = Regex.Match(html, @"(?isn)(\s*<th>\W+(?<column>\w+?)\W+</th>)+\s*?</tr>.+?(?<row><tr>(\s*?<td>\W*(?<item>\w*)\W*?</td>)+\s+?</tr>\s*)+");
                List<string> columns = new List<string>();
                foreach (Capture c in m.Groups["column"].Captures)
                {
                    columns.Add(c.Value);
                }
                List<List<string>> rows = new List<List<string>>();
                for (int i = 0; i < m.Groups["row"].Captures.Count; i++)
                {
                    List<string> row = new List<string>();
                    for (int j = i * columns.Count; j < (i + 1) * columns.Count; j++)
                    {
                        row.Add(m.Groups["item"].Captures[j].Value);
                    }
                    rows.Add(row);
                }
                foreach (string column in columns)
                {
                    Console.Write(column + "\t");
                }
                Console.WriteLine();
                foreach (List<string> row in rows)
                {
                    foreach (string item in row)
                    {
                        Console.Write(item + "\t");
                    }
                    Console.WriteLine();
                }
            }输出列头1   列头2   列头3   列头4   列头5
    值1     值1     值1     值1     值1
    值1     值1     值1     值1     值1