给定以下字符串我需要找到字符串的最后一次出现[12] Solution Confirmed然后向后遍历直到我达到日期。 日期将始终采用dd-MM-yyyy格式。
<tr><td>17-05-2011 16:28 </td><td>DB </td> <td>(YB) 0 </td><td>75% </td><td> </td> <td>[10] Pending - Probable</td></tr><tr><td>15-05-2011 22:40 </td> <td>YB </td><td>(YB) 0 </td><td>90% </td><td> </td> <td>[12] Solution Confirmed</td></tr>在上面的例子中,我期望的日期是15-05-2011。
<tr><td>18-07-2011 10:10 </td> <td>YB </td><td>(YB) 56650 </td> <td>90% </td><td> </td><td>[12] Solution Confirmed</td></tr>在上面的例子中,我预计日期将是18-07-2011
我无法100%确定我所看到的字符串是否符合HTML标准。 正则表达式最适合我吗? 谁能提供一个有效的例子。
编辑我已经调查了这个,看起来日期总是采用这种格式...
<td>dd-MM-yyyy HH:mm </td>Given the following string I need to find the last occurance of the string [12] Solution Confirmed then traverse backwards until I hit a date. The date will always be in the format dd-MM-yyyy.
<tr><td>17-05-2011 16:28 </td><td>DB </td> <td>(YB) 0 </td><td>75% </td><td> </td> <td>[10] Pending - Probable</td></tr><tr><td>15-05-2011 22:40 </td> <td>YB </td><td>(YB) 0 </td><td>90% </td><td> </td> <td>[12] Solution Confirmed</td></tr>In the example above the date I would expect would be 15-05-2011.
<tr><td>18-07-2011 10:10 </td> <td>YB </td><td>(YB) 56650 </td> <td>90% </td><td> </td><td>[12] Solution Confirmed</td></tr>In the example above I would expect the date would be 18-07-2011
I can't be 100% sure that the string I am looking at is HTML compliant. Would a Regex suit me best? Can anyone provide a working example.
edit I have looked into this and it looks like the date is always in this format...
<td>dd-MM-yyyy HH:mm </td>最满意答案
我在控制台应用程序中确认了这一点,但我的想法和@Jason一样:
string x = "<tr><td>17-05-2011 16:28 </td><td>DB </td><td>(YB) 0 </td><td>75% </td>" + "<td> </td><td>[10] Pending - Probable</td></tr><tr><td>15-05-2011 22:40 </td>" + "<td>YB </td><td>(YB) 0 </td><td>90% </td><td> </td>" + "<td>[12] Solution Confirmed</td></tr>"; int searchBeforeLocation = x.LastIndexOf("Solution Confirmed"); x = x.Substring(0, searchBeforeLocation); Regex r = new Regex(@"\d{2}-\d{2}-\d{4}"); MatchCollection matches = r.Matches(x); int matchCount = matches.Count; Console.WriteLine(matches[matches.Count - 1].Value); Console.Read();离“解决方案确认”最近的那个将是最后一场比赛
I was confirming this in a console app but my thinking was same as @Jason:
string x = "<tr><td>17-05-2011 16:28 </td><td>DB </td><td>(YB) 0 </td><td>75% </td>" + "<td> </td><td>[10] Pending - Probable</td></tr><tr><td>15-05-2011 22:40 </td>" + "<td>YB </td><td>(YB) 0 </td><td>90% </td><td> </td>" + "<td>[12] Solution Confirmed</td></tr>"; int searchBeforeLocation = x.LastIndexOf("Solution Confirmed"); x = x.Substring(0, searchBeforeLocation); Regex r = new Regex(@"\d{2}-\d{2}-\d{4}"); MatchCollection matches = r.Matches(x); int matchCount = matches.Count; Console.WriteLine(matches[matches.Count - 1].Value); Console.Read();The one nearest to the "Solution Confirmed" will be the last match
更多推荐
发布评论