这是信息:
<tr><td height=27 align=center>现货铂金</td><td height=18 align=center>289.26</td><td height=18 align=center class=fontok>-1.04</td><td height=18 align=center>-0.36%</td><td height=18 align=center>290.61</td><td height=18 align=center>288.86</td></tr><tr><td height=27 align=center>现货钯金</td><td height=18 align=center>117.83</td><td height=18 align=center class=fontok>-0.55</td><td height=18 align=center>-0.46%</td><td height=18 align=center>118.48</td><td height=18 align=center>117.57</td></tr><tr><td height=27 align=center>现货白银</td><td height=18 align=center>5482</td><td height=18 align=center class=fontok>-21</td><td height=18 align=center>-0.38%</td><td height=18 align=center>5524</td><td height=18 align=center>5470</td></tr>
我想获取<tr><td height=27 align=center>现货铂金</td><td height=18 align=center>289.26</td><td height=18 align=center>-0.36%</td><tr>这样的数据,怎么做啊?我自己写的正则表达式,只能获取这一行,还显示两次。哪位大牛帮忙解决下啊,不胜感激

解决方案 »

  1.   


                string str = @"<tr><td height=27 align=center>现货铂金</td><td height=18 align=center>289.26</td><td height=18 align=center class=fontok>-1.04</td><td height=18 align=center>-0.36%</td><td height=18 align=center>290.61</td><td height=18 align=center>288.86</td></tr><tr><td height=27 align=center>现货钯金</td><td height=18 align=center>117.83</td><td height=18 align=center class=fontok>-0.55</td><td height=18 align=center>-0.46%</td><td height=18 align=center>118.48</td><td height=18 align=center>117.57</td></tr><tr><td height=27 align=center>现货白银</td><td height=18 align=center>5482</td><td height=18 align=center class=fontok>-21</td><td height=18 align=center>-0.38%</td><td height=18 align=center>5524</td><td height=18 align=center>5470</td></tr>";
                Regex reg = new Regex(@"(?is)<tr>(?:\s*<td[^>]*?>(.*?)</td>)*</tr>");
                foreach (Match m in reg.Matches(str))
                {
                    foreach (Capture c in m.Groups[1].Captures)
                        Console.WriteLine(c.Value);
                    Console.WriteLine(new string('*', 20));
                }
    /*
    现货铂金
    289.26
    -1.04
    -0.36%
    290.61
    288.86
    ********************
    现货钯金
    117.83
    -0.55
    -0.46%
    118.48
    117.57
    ********************
    现货白银
    5482
    -21
    -0.38%
    5524
    5470
    ********************
    */
      

  2.   

    这个你用dom获取子节点不就行了吗,和正则表达式有什么关系啊,还是你的问题不是很清楚,我没有在代码中看到正则表达式啊?
      

  3.   

    其实2楼已经给了string input=@"Html源码";
    foreach (Match m in reg.Matches(input,@"(?is)<tr>(<td[^>]*?>(.*?)</td>){1,}</tr>"))
                {
                    foreach (Capture c in m.Groups[1].Captures)
                        Console.WriteLine(c.Value);
                }
      

  4.   

    改一下string input=@"Html源码";
    foreach (Match m in Regex.Matches(input,@"(?is)<tr>(<td[^>]*?>(.*?)</td>){1,}</tr>"))
                {
                    foreach (Capture c in m.Groups[1].Captures)
                        Console.WriteLine(c.Value);
                }
      

  5.   

    我只是想输出:
    <tr><td height=27 align=center>现货铂金</td><td height=18 align=center>289.26</td><td height=18 align=center>-0.36%</td><tr>
      

  6.   


    try            {                                Regex regexMobile = new Regex(@"<tr><td height=27 align=center>(.*?)</td><td height=18 align=center>(.*?)</td><td height=18 align=center class=fontok>(.*?)</td><td height=18 align=center>(.*?)</td><td height=18 align=center>290.61</td><td height=18 align=center>(.*?)</td></tr>",RegexOptions.Singleline);                Match matchMobile = regexMobile.Match(strMobile);                while (matchMobile.Success)                {                    htmlMobile += @"<tr>";                    htmlMobile += @"<td align=""center"">" + matchMobile.Groups[1].Value + "</td>";                    htmlMobile += @"<td align=""center"">" + matchMobile.Groups[2].Value + "</td>";                    htmlMobile += @"<td align=""center"">" + matchMobile.Groups[4].Value + "</td>";                    htmlMobile += @"</tr>";                    matchMobile = matchMobile.NextMatch();                }                strMobile = null;                matchMobile = null;            }            catch (ArgumentException ex)            {            }                       Response.Write(htmlMobile);
      

  7.   


    try
      {                                
        Regex regexMobile = new Regex(@"<tr><td height=27 align=center>(.*?)</td><td height=18 align=center>(.*?)</td><td height=18 align=center class=fontok>(.*?)</td><td height=18 align=center>(.*?)</td><td height=18 align=center>290.61</td><td height=18 align=center>(.*?)</td></tr>",RegexOptions.Singleline);                     
        Match matchMobile = regexMobile.Match(strMobile);                
        while (matchMobile.Success)                
        {                    
           htmlMobile += @"<tr>";                    
           htmlMobile += @"<td align=""center"">" + matchMobile.Groups[1].Value + "</td>";                    
           htmlMobile += @"<td align=""center"">" + matchMobile.Groups[2].Value + "</td>";                    
           htmlMobile += @"<td align=""center"">" + matchMobile.Groups[4].Value + "</td>";                    
           htmlMobile += @"</tr>";                    
           matchMobile = matchMobile.NextMatch();               
    }                
    strMobile = null;               
    matchMobile = null;            
    }            
    catch (ArgumentException ex){ }                       
    Response.Write(htmlMobile);
      

  8.   

     string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt
                    string pattern = @"^(?i)(<tr[^>]*?>(<td[^>]*?>[^<]*?</td>){2})((?!</tr>).)*?(<td[^>]*?>[^<]*?%[^<]*?</td>).*?(</tr>)";
                    tempStr = Regex.Replace(Regex.Match(tempStr,pattern).Value, pattern, @"$1$4$5");
                    /*
                    <tr><td height=27 align=center>现货铂金</td><td height=18 align=center>289.26</td><td height=18 align=center>-0.36%</td></tr>                 */
      

  9.   

    循环写成
    foreach(Match matchMobile in regexMobile.Matches(strMobile))
    循环体中去掉
    matchMobile = matchMobile.NextMatch();
      

  10.   


    不只是显示这一条数据,剩下两个<tr></tr>都显示
      

  11.   


      string tempStr = File.ReadAllText(@"C:\Documents and Settings\Administrator\桌面\Test.txt", Encoding.GetEncoding("GB2312"));//读取txt
                    string pattern = @"(?i)(<tr[^>]*?>(<td[^>]*?>[^<]*?</td>){2})((?!</tr>).)*?(<td[^>]*?>[^<]*?%[^<]*?</td>).*?(</tr>)";
                    string[] tempStr_list = Regex.Matches(tempStr, pattern).Cast<Match>().Select(a=>Regex.Replace(a.Value,pattern,"$1$4$5")).ToArray();
                    /*
                      [0] "<tr><td height=27 align=center>现货铂金</td><td height=18 align=center>289.26</td><td height=18 align=center>-0.36%</td></tr>" string
                    [1] "<tr><td height=27 align=center>现货钯金</td><td height=18 align=center>117.83</td><td height=18 align=center>-0.46%</td></tr>" string
                    [2] "<tr><td height=27 align=center>现货白银</td><td height=18 align=center>5482</td><td height=18 align=center>-0.38%</td></tr>" string                 */