<div>
<table cellspacing="0" rules="all" border="1" id="gvRouteFirendTime" style="width:100%;border-collapse:collapse;">
<tr class="td_gridView_header">
<th scope="col" style="white-space:nowrap;">开始日期</th><th scope="col" style="white-space:nowrap;">结束日期</th><th scope="col" style="white-space:nowrap;">周开始</th><th scope="col" style="white-space:nowrap;">周结束</th><th scope="col" style="white-space:nowrap;">首班车时间</th><th scope="col" style="white-space:nowrap;">末班车时间</th>
</tr><tr class="td_gridView_row">
<td style="white-space:nowrap;">11-01</td><td style="white-space:nowrap;">12-31</td><td style="white-space:nowrap;">星期一</td><td style="white-space:nowrap;">星期五</td><td style="white-space:nowrap;">05:30</td><td style="white-space:nowrap;">23:30</td>
</tr><tr class="td_gridView_row_alternate">
<td style="white-space:nowrap;">11-01</td><td style="white-space:nowrap;">12-31</td><td style="white-space:nowrap;">星期六</td><td style="white-space:nowrap;">星期日</td><td style="white-space:nowrap;">05:30</td><td style="white-space:nowrap;">23:30</td>
</tr>
</table>
</div>
                                                                </td>
                                                            </tr>
                                                        </table>
                                                    </fieldset>
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <fieldset>
                                                        <legend>发车间隔有上面这样一个结构的字符串现在要利用正则表达式获取首班车时间(05:30)和末班车时间(23:30)  请问正则表达式怎么写

解决方案 »

  1.   

    "\b(?<n1>\d{1,3})\.(?<n2>\d{1,3})\.(?<n3>\d{1,3})\.(?<n4>\d{1,3}\b)"
      

  2.   


    string content = @"<div>
        <table cellspacing=""0"" rules=""all"" border=""1"" id=""gvRouteFirendTime"" style=""width:100%;border-collapse:collapse;"">
        <tr class=""td_gridView_header"">
        <th scope=""col"" style=""white-space:nowrap;"">开始日期</th><th scope=""col"" style=""white-space:nowrap;"">结束日期</th><th scope=""col"" style=""white-space:nowrap;"">周开始</th><th scope=""col"" style=""white-space:nowrap;"">周结束</th><th scope=""col"" style=""white-space:nowrap;"">首班车时间</th><th scope=""col"" style=""white-space:nowrap;"">末班车时间</th>
        </tr><tr class=""td_gridView_row"">
        <td style=""white-space:nowrap;"">11-01</td><td style=""white-space:nowrap;"">12-31</td><td style=""white-space:nowrap;"">星期一</td><td style=""white-space:nowrap;"">星期五</td><td style=""white-space:nowrap;"">05:30</td><td style=""white-space:nowrap;"">23:30</td>
        </tr><tr class=""td_gridView_row_alternate"">
        <td style=""white-space:nowrap;"">11-01</td><td style=""white-space:nowrap;"">12-31</td><td style=""white-space:nowrap;"">星期六</td><td style=""white-space:nowrap;"">星期日</td><td style=""white-space:nowrap;"">05:30</td><td style=""white-space:nowrap;"">23:30</td>
        </tr>
        </table>
        </div>
          </td>
          </tr>
          </table>
          </fieldset>
          </td>
          </tr>
          <tr>
          <td>
          <fieldset>
          <legend>发车间隔";//重要:考虑到html源码是来源于网络,其中可能包含很多回车换行符
    //为了匹配成功,把回车换行符删掉
    content = Regex.Replace(content, "\r", "");
    content = Regex.Replace(content, "\n", "");//开始日期
    string patternStart = @"(?i)(?<=<td style=""white-space:nowrap;"">)\d{2}:\d{2}(?=</td><td)";
    Regex regexStart = new Regex(patternStart);Response.Write("开始日期:");
    foreach (Match match in regexStart.Matches(content))
    {
        Response.Write(match.Value + " ");  //开始日期:05:30 05:30 
    }//结束日期
    string patternEnd = @"(?mi)(?<=<td style=""white-space:nowrap;"">)\d{2}:\d{2}(?=</td>\s*</tr)";
    System.Text.RegularExpressions.Regex regexEnd = new System.Text.RegularExpressions.Regex(patternEnd);Response.Write("<br />结束日期:");
    foreach (Match match in regexEnd.Matches(content))
    {
        Response.Write(match.Value + " ");  //结束日期:23:30 23:30 
    }
      

  3.   

    string s = File.ReadAllText(Server.MapPath("~/test.txt"));
    MatchCollection matches = Regex.Matches(s, @"<td[^>]*?>\d{1,2}-\d{1,2}</td>.+>(\d{2}:\d{2})</td><td[^>]*?>(\d{2}:\d{2})");
    foreach (Match match in matches)
    {
    Response.Write(match.Groups[1].Value + "<br/>");
    Response.Write(match.Groups[2].Value + "<br/></br>");
    }
      

  4.   

            string str = @"<div>
        <table cellspacing=""0"" rules=""all"" border=""1"" id=""gvRouteFirendTime"" style=""width:100%;border-collapse:collapse;"">
        <tr class=""td_gridView_header"">
        <th scope=""col"" style=""white-space:nowrap;"">开始日期</th><th scope=""col"" style=""white-space:nowrap;"">结束日期</th><th scope=""col"" style=""white-space:nowrap;"">周开始</th><th scope=""col"" style=""white-space:nowrap;"">周结束</th><th scope=""col"" style=""white-space:nowrap;"">首班车时间</th><th scope=""col"" style=""white-space:nowrap;"">末班车时间</th>
        </tr><tr class=""td_gridView_row"">
        <td style=""white-space:nowrap;"">11-01</td><td style=""white-space:nowrap;"">12-31</td><td style=""white-space:nowrap;"">星期一</td><td style=""white-space:nowrap;"">星期五</td><td style=""white-space:nowrap;"">05:30</td><td style=""white-space:nowrap;"">23:30</td>
        </tr><tr class=""td_gridView_row_alternate"">
        <td style=""white-space:nowrap;"">11-01</td><td style=""white-space:nowrap;"">12-31</td><td style=""white-space:nowrap;"">星期六</td><td style=""white-space:nowrap;"">星期日</td><td style=""white-space:nowrap;"">05:30</td><td style=""white-space:nowrap;"">23:30</td>
        </tr>
        </table>
        </div>
          </td>
          </tr>
          </table>
          </fieldset>
          </td>
          </tr>
          <tr>
          <td>
          <fieldset>
          <legend>发车间隔";
            Regex reg = new Regex(@"(?is)(?<=<table[^>]*?id=""gvRouteFirendTime""[^>]*?>(?:(?!</?table).)*)<td[^>]*?>(\d{1,2}:\d{1,2})</td>");
            foreach (Match m in reg.Matches(str))
                Response.Write(m.Groups[1].Value+"<br/>");