当前位置: 代码迷 >> ASP.NET >> 利用正则表达式获取指定的值,该怎么解决
  详细解决方案

利用正则表达式获取指定的值,该怎么解决

热度:5857   发布时间:2013-02-25 00:00:00.0
利用正则表达式获取指定的值
<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) 请问正则表达式怎么写

------解决方案--------------------------------------------------------
"\b(?<n1>\d{1,3})\.(?<n2>\d{1,3})\.(?<n3>\d{1,3})\.(?<n4>\d{1,3}\b)"

------解决方案--------------------------------------------------------
C# code
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 }