网页数据事先保存在TXT文件htmlcontent.txt里,l内容比较复杂,如下所示
<html>
<head>
<title>FT_result</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link rel="stylesheet" href="/style/member/mem_body.css" type="text/css">
</head><body id="MFT" onSelectStart="self.event.returnValue=false" oncontextmenu="self.event.returnValue=false">
<table border="0" cellpadding="0" cellspacing="0" id="box">
  <tr>
    <td id="ad"><span class="real_msg"><marquee scrolldelay="120">敬请注意:11/22 ( 智利国家队 VS 圣地牙哥漫游者)因赛事提前至17:15开赛,所有投注在17:14:59之後的注单一律取消,过关以(1)计算..(走地除外).</marquee></span>
<p><a href="javascript://" onClick="javascript: window.open('../scroll_history.php?uid=55f04ce0m1a61e2l790d9&langx=zh-cn','','menubar=no,status=yes,scrollbars=yes,top=150,left=200,toolbar=no,width=510,height=500')">历史讯息</a></p></td>
  </tr>
  <tr>
    <td class="top">
      <form name="game_result" action="result.php?uid=55f04ce0m1a61e2l790d9" method=POST>
      <input type=HIDDEN name="game_type" value="FT">
      <h1><em>足球赛事结果</em>
        <span class="rig"><a href="/app/member/result/result.php?game_type=FT&list_date=2006-11-21&uid=55f04ce0m1a61e2l790d9">昨日</a>  / <a href="/app/member/result/result.php?game_type=FT&today=2006-11-23&uid=55f04ce0m1a61e2l790d9">明日</a>
  <input id="today_gmt" type=TEXT name="today" value="2006-11-22" size="9" maxlength="10" class="txt">
        <input type="submit" value="查询" name="submit"></span>
        </h1>
  </form>
</td>
  </tr>
  <tr>
    <td class="mem">
      <table border="0" cellspacing="1" cellpadding="0" class="game">
        <tr> 
          <th class="time">时间</th>
          <th width="6%">场次</th>
          <th width="71%">比赛队伍</th>
          <th width="7%">半场</th>
          <th width="7%">全场</th>
        </tr>
                <tr > 
          <td colspan="5" class="b_hline"> 爱尔兰甲组附加赛</td>
        </tr>
        <tr  class="b_cen" > 
          <td >11-22<br/>3:45p</td>
          <td >3193<br/>3194</td>
          <td align="left"  >屈福特联  &nbsp;&nbsp;<br/>登克尔克  &nbsp;&nbsp;</td>
          <td ><font color="#CC0000"><b><span style="overflow:hidden;width:50;height:15">1</span><br/><span style="overflow:hidden;width:50;height:15">0</span></b></font></td>
          <td ><font color="#CC0000"><b><span style="overflow:hidden;width:50;height:15">1</span><br/><span style="overflow:hidden;width:50;height:15">1</span></b></font></td>
        </tr>
        <tr > 
          <td colspan="5" class="b_hline"> 苏格兰甲组</td>
        </tr>
        <tr  class="b_cen" > 
          <td >11-22<br/>3:30p</td>
          <td >3025<br/>3026</td>
          <td align="left"  >加迪  &nbsp;&nbsp;<br/>巴特里  &nbsp;&nbsp;</td>
          <td ><font color="#CC0000"><b><span style="overflow:hidden;width:50;height:15">0</span><br/><span style="overflow:hidden;width:50;height:15">0</span></b></font></td>
          <td ><font color="#CC0000"><b><span style="overflow:hidden;width:50;height:15">1</span><br/><span style="overflow:hidden;width:50;height:15">1</span></b></font></td>
        </tr>
        <tr  class="b_cen" > 
          <td >11-22<br/>3:45p</td>
          <td >3167<br/>3168</td>
          <td align="left"  >圣庄士东  &nbsp;&nbsp;<br/>罗斯郡  &nbsp;&nbsp;</td>
          <td ><font color="#CC0000"><b><span style="overflow:hidden;width:50;height:15">2</span><br/><span style="overflow:hidden;width:50;height:15">1</span></b></font></td>
          <td ><font color="#CC0000"><b><span style="overflow:hidden;width:50;height:15">2</span><br/><span style="overflow:hidden;width:50;height:15">2</span></b></font></td>
        </tr>
      </table> 
</td>
  </tr>
  <tr><td id="foot"><b>&nbsp;</b></td></tr>
</table>
<div id="copyright">
    版所有 皇冠 建议您以 IE 5.0 800 X 600 以上高彩模式浏览本站&nbsp;&nbsp;<a id=download title="下载" href="http://www.microsoft.com/taiwan/products/ie/" target="_blank">立刻下载IE</a>
</div>
</body>
</html>上面里这样的<tr></tr>是有N个但内容不重复的
<tr  class="b_cen" > 
          <td >11-22<br/>3:45p</td>
          <td >3167<br/>3168</td>
          <td align="left"  >圣庄士东  &nbsp;&nbsp;<br/>罗斯郡  &nbsp;&nbsp;</td>
          <td ><font color="#CC0000"><b><span style="overflow:hidden;width:50;height:15">2</span><br/><span style="overflow:hidden;width:50;height:15">1</span></b></font></td>
          <td ><font color="#CC0000"><b><span style="overflow:hidden;width:50;height:15">2</span><br/><span style="overflow:hidden;width:50;height:15">2</span></b></font></td>
        </tr>
我现在想得到这样<tr></tr>里的具体如3167、3168、圣庄士东、罗斯郡、2、1、2、2的信息,如何利用正则表达表循环得到呢?

解决方案 »

  1.   

    function f(s){
    //s这转入HTML字符串
    s=s.replace(/<br[\/]?>/gi,","); //把<br>换成逗号re1=/<tr[^>]*class\s*=\s*["|']?b_cen["|']?[^>]*>\s*(<td[^>]*>([\s\S]*?)<\/td>)*\s*<\/tr>/gi;//\s*(<td[^>*]>([\s\S]*?)<\/td>)*\s*arr=s.match(re1);   //得到class=b_cen的<tr>...</tr>数组re2=/(<[\/]?\w*[^>]*>\s*)|&nbsp|\s/gi;for(i=0;i<arr.length;i++)
    {
    arr[i]=arr[i].replace(re1,"$1"); //得到<td>...</td>串
    arr[i]=arr[i].replace(/<\/td>/gi,",") //把</td>换成逗号
    arr[i]=arr[i].replace(re2,""); //把所有<..>或&nbsp;及空白字符(如回车换行等) 换成空
    }
    alert(arr); //这样就得到以“,”分割的所有值。
    }
      

  2.   

    楼上j9988(j9988)是javascript的写法吧?
      

  3.   

    C#:
    ------------------------------------------------
    StreamReader sr = new StreamReader(@"a.html", System.Text.Encoding.Default);
    string mystr=sr.ReadToEnd().ToString();
    mystr = this.getValue(mystr);
    MessageBox.Show(mystr);
                
     
            private string getValue(string content)
            {            Regex re = new Regex(@"<tr[^>]*class\s*=\s*[""|']?b_cen[""|']?[^>]*>\s*(?<val>(<td[^>]*>([\s\S]*?)</td>)*)\s*</tr>", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                MatchCollection mc = re.Matches(content);
                string str = "";
                if (mc.Count > 0)
                {
                    for (int i = 0; i < mc.Count; i++)
                    {
                        string s = mc[i].Groups["val"].Value;
                        s = Regex.Replace(s,@"<br[/]?>",",",RegexOptions.Compiled|RegexOptions.IgnoreCase);
                        s = Regex.Replace(s, @"</td>", ",", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                        s = Regex.Replace(s,@"(<[/]?\w*[^>]*>\s*)|&nbsp;|\s", "", RegexOptions.Compiled | RegexOptions.IgnoreCase);
                        str = str + s;
                    }
                }
                return str;
            }