我已经从网页中获取到一个table,存成了string str="<table...../table>",请高手指点一下怎么用正则表达式取出每一行每一格的数据.正则表达式我还没用过,请高手详细指点

解决方案 »

  1.   

    把" <table...../table>"里的代码发下看看
      

  2.   

    <td[^>]*>[^<>]+</td>
      

  3.   

    把你的str具体内容发出来。。
    要提取哪些内容也写一下。。
      

  4.   

    string str = "<table.....</table>";
    Regex reg = new Regex(@"\<td.*?\>.+?\<\/td\>", RegexOptions.IgnoreCase);
    Match mat = reg.Match(str);
    while(mat.Success)
    {
    Response.Write(mat.Value);
    mat = reg.Match(str, mat.Index+mat.Length);
    }
      

  5.   

    非常感谢大家的热情,str是一个标准的table
    "<table width=\"98%\" border=\"1\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bordercolor=\"#999999\">\r\n                            <tr bgcolor=\"#CCCCCC\">\r\n                              <td width=\"10%\" height=\"30\">\r\n                                <div align=\"center\">品种代码</div></td>\r\n                              <td width=\"10%\" height=\"30\">\r\n                                <div align=\"center\">a</div></td>\r\n                              <td width=\"10%\" height=\"30\">\r\n                                <div align=\"center\">b</div></td>\r\n                              <td width=\"10%\" height=\"30\">\r\n                                <div align=\"center\">c</div></td>\r\n                              <td width=\"10%\" height=\"30\">\r\n                                <div align=\"center\">d</div></td>\r\n                              <td width=\"10%\" height=\"30\">\r\n                                <div align=\"center\">e</div></td>\r\n                              <td width=\"10%\" height=\"30\">\r\n                                <div align=\"center\">f</div></td>\r\n                              <td width=\"10%\" height=\"30\">\r\n                                <div align=\"center\">g</div></td>\r\n                              <td width=\"10%\" height=\"30\" align=\"center\">\r\n                                <div align=\"center\"> h</div></td>\r\n                              <td width=\"10%\" height=\"30\">\r\n                                <div align=\"center\"> i </div></td>\r\n                            </tr>\r\n                                                        <tr bgcolor=\"#FFFFFF\">\r\n                              <td height=\"25\" width=\"10%\" align=\"center\">00001</td>\r\n                              <td height=\"25\" width=\"10%\" align=\"center\">3080</td>\r\n                              <td height=\"25\" align=\"center\" width=\"10%
    我是想把每一个品种对应的值取出来放在数据库里面
      

  6.   

    (?<=<div [^>]*>)[^<]+如果不要匹配列名 品种代码,那就:
    (?<=<div [^>]*>)((?!品种代码)[^<])+(?=</div>)
      

  7.   

    谢谢min_jie,谢谢大家 我把几位的综合了一下
                Regex reg = new Regex("(?<=<div [^>]*>)((?!品种代码)[^<])+(?=</div>)", RegexOptions.IgnoreCase);
                Match mat = reg.Match(str);
                while (mat.Success)
                {
                    Console.WriteLine(mat.Value.ToString());
                    mat = reg.Match(str, mat.Index + mat.Length);
                }
    只列出了第一行就完了,请继续指点
      

  8.   

                Regex reg = new Regex("(? <= <div [^>]*>)((?!品种代码)[^ <])+(?= </div>)", RegexOptions.IgnoreCase); 
                Match mat = reg.Match(str); 
                while (mat.Success) 
                { 
                    Console.WriteLine(mat.Value.ToString()); 
                    mat = reg.Match(str, mat.Index + mat.Length); //这句是要干什么?
                } 可以这样写:
    Regex reg = new Regex("(? <= <div [^>]*>)((?!品种代码)[^ <])+(?= </div>)", RegexOptions.IgnoreCase); 
    foreach(Match m in reg.Matchs(str))
    {
     Console.WriteLine(m.Value);
    }
      

  9.   


    你还是要把具体的网页代码发出来啊。。要不谁能知道都有哪些列,都有那些规则呢?
    如果是很多列,建议用捕获组来实现。<div [^>]*>(?<type>[^<]+)</div>[\s\S]*?<div [^>]*>(?<name>[^<]+)</div>
    类似这样的。。
      

  10.   

    谢谢,直接复制Regex reg = new Regex("(? <= <div [^>]*>)((?!品种代码)[^ <])+(?= </div>)", RegexOptions.IgnoreCase); 
    foreach(Match m in reg.Match(str))
    {
     Console.WriteLine(m.Value);
    }
    运行错误 2
    “System.Text.RegularExpressions.Match”不包含“GetEnumerator”的公共定义,因此 foreach 语句不能作用于“System.Text.RegularExpressions.Match”类型的变量 F:\dotnet\ocs\test\test\test\Form1.cs 190 17 test
      

  11.   

    少写了个s,不好意思reg.Matchs(str) 
      

  12.   

    呵呵,reg.Matchs(str) 错误 2 “System.Text.RegularExpressions.Regex”并不包含“Matchs”的定义
    刚开始学习C#...
      

  13.   

    郁闷啊。。手打的代码。。没怎么注意:
    reg.Matches(str)
    这回对了。。
      

  14.   

    谢谢,奇怪了,刚才还好好的,现在运行到Regex reg = new Regex...又冒出来无法识别的分组构造
      

  15.   


    CSDN的<号总是会自动在前面加一个空格,注意去掉那个空格。。
      

  16.   

    谢谢,现在没报错了,但始终是只会显示出第一行来,也就是只显示表头来,后面的数据00001,00002等都没出来
    Regex reg = new Regex("(? <= <div [^>]*>)((?!品种代码)[^ <])+(?= </div>)", RegexOptions.IgnoreCase); 
    foreach(Match m in reg.Match(str)) 

    Console.WriteLine(m.Value); 

    表头每一列实际是长的中文,我把他简化成了a,b,c...不知道有没有影响到各位专家的判断
      

  17.   

    我测试的结果是:







     h 
     i 
    有问题吗?
    如果是还有其他的列,那正则表达式肯定要把其他列也给加上。。
      

  18.   

    对的,表结构大致如下,参考上面贴出的部分
    a  b  c  d  e  f  g
    01 4  5  6   7 8   9
    02 4  4  ......
    我的意思可能没表达清楚,其实是想取出01对应4 5 6 7 8 9 的值 02对应的4 4 等每一个td的值
    上面的代码只是把表头abcdefg 取出来
    行列是固定的
      

  19.   

    能不能请高手指点一下,通用的操作,比如我要取01对应的b c ... 02 对应的b c....
    我对正则表达式基本上不懂,谢谢 min_jie 的耐心
      

  20.   

    谢谢,不是不愿意,是想着代码太长了,没能把意思表大清楚,
    网址是http://www.smm.com.cn/lme.php,table我已经存在string str里面了,想把里面几种金属对应的数据存在数据库里
    再次谢谢min_jie 
      

  21.   

    string pattern="<tr [^>]*>\s*<td [^>]*>(?<type>[^<]*)</td>\s*<td [^>]*>(?<open>[^<]*)</td>\s*<td [^>]*>(?<high>[^<]*)</td>\s*<td [^>]*>(?<low>[^<]*)</td>\s*<td [^>]*>(?<close>[^<]*)</td>\s*<td [^>]*>(?<nowprice>[^<]*)</td>\s*<td [^>]*>(?<balance>[^<]*)</td>\s*<td [^>]*>(?<trade>[^<]*)</td>\s*<td [^>]*>(?<count>[^<]*)</td>\s*<td [^>]*><font [^>]*>(?<addcount>[^<]*)</font></td>\s*</tr>";
    Regex reg=new Regex(pattern);
    foreach(Match m in reg.Matches(str))
    {
     Console.WriteLine(m.Group["type"].Value);
     Console.WriteLine(m.Group["open"].Value);
     Console.WriteLine(m.Group["high"].Value);
     Console.WriteLine(m.Group["low"].Value);
     Console.WriteLine(m.Group["close"].Value);
     Console.WriteLine(m.Group["nowprice"].Value);
     Console.WriteLine(m.Group["balance"].Value);
     Console.WriteLine(m.Group["trade"].Value);
     Console.WriteLine(m.Group["count"].Value);
     Console.WriteLine(m.Group["addcount"].Value);
    }
      

  22.   

    谢谢min_jie ,复制过去,改了一小点(Groups) 终于搞定了,
    还没能看懂 要好好学学正则了