在匹配前的字符串中查找日期(Find a date within a string that is before a match)

编程入门 行业动态 更新时间:2024-10-28 19:28:31
在匹配前的字符串中查找日期(Find a date within a string that is before a match)

给定以下字符串我需要找到字符串的最后一次出现[12] Solution Confirmed然后向后遍历直到我达到日期。 日期将始终采用dd-MM-yyyy格式。

<tr><td>17-05-2011&nbsp;16:28&nbsp;</td><td>DB&nbsp;</td> <td>(YB)&nbsp;0&nbsp;</td><td>75%&nbsp;</td><td>&nbsp;</td> <td>[10] Pending - Probable</td></tr><tr><td>15-05-2011&nbsp;22:40&nbsp;</td> <td>YB&nbsp;</td><td>(YB)&nbsp;0&nbsp;</td><td>90%&nbsp;</td><td>&nbsp;</td> <td>[12] Solution Confirmed</td></tr>

在上面的例子中,我期望的日期是15-05-2011。

<tr><td>18-07-2011&nbsp;10:10&nbsp;</td> <td>YB&nbsp;</td><td>(YB)&nbsp;56650&nbsp;</td> <td>90%&nbsp;</td><td>&nbsp;</td><td>[12] Solution Confirmed</td></tr>

在上面的例子中,我预计日期将是18-07-2011

我无法100%确定我所看到的字符串是否符合HTML标准。 正则表达式最适合我吗? 谁能提供一个有效的例子。

编辑我已经调查了这个,看起来日期总是采用这种格式...

<td>dd-MM-yyyy&nbsp;HH:mm&nbsp;</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&nbsp;16:28&nbsp;</td><td>DB&nbsp;</td> <td>(YB)&nbsp;0&nbsp;</td><td>75%&nbsp;</td><td>&nbsp;</td> <td>[10] Pending - Probable</td></tr><tr><td>15-05-2011&nbsp;22:40&nbsp;</td> <td>YB&nbsp;</td><td>(YB)&nbsp;0&nbsp;</td><td>90%&nbsp;</td><td>&nbsp;</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&nbsp;10:10&nbsp;</td> <td>YB&nbsp;</td><td>(YB)&nbsp;56650&nbsp;</td> <td>90%&nbsp;</td><td>&nbsp;</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&nbsp;HH:mm&nbsp;</td>

最满意答案

我在控制台应用程序中确认了这一点,但我的想法和@Jason一样:

string x = "<tr><td>17-05-2011&nbsp;16:28&nbsp;</td><td>DB&nbsp;</td><td>(YB)&nbsp;0&nbsp;</td><td>75%&nbsp;</td>" + "<td>&nbsp;</td><td>[10] Pending - Probable</td></tr><tr><td>15-05-2011&nbsp;22:40&nbsp;</td>" + "<td>YB&nbsp;</td><td>(YB)&nbsp;0&nbsp;</td><td>90%&nbsp;</td><td>&nbsp;</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&nbsp;16:28&nbsp;</td><td>DB&nbsp;</td><td>(YB)&nbsp;0&nbsp;</td><td>75%&nbsp;</td>" + "<td>&nbsp;</td><td>[10] Pending - Probable</td></tr><tr><td>15-05-2011&nbsp;22:40&nbsp;</td>" + "<td>YB&nbsp;</td><td>(YB)&nbsp;0&nbsp;</td><td>90%&nbsp;</td><td>&nbsp;</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

更多推荐

本文发布于:2023-08-02 04:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1369884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   日期   Find   match   string

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!