大于日期格式 xx-xx-xxxx 的正则表达式
我有一个包含 40,000 条记录的文本文件。我需要找到大于 2011 年 10 月 1 日的所有项目。格式为 01-10-2011 - 如何使用正则表达式执行此操作?
I have a single text file with 40,000 records. I need to locate all items greater than October 1st 2011. The format is 01-10-2011 - How can I do this using regular expression?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它可能不应该这样做,但可以这样做:
假设所有日期都是 DD-MM-YYYY 并且有效,并且您不需要查找 2019 年以后的日期,因此可以对其进行调整如果需要的话。
在 Dreamweaver CS5 中进行了测试,我怀疑他们随着时间的推移已经对正则表达式引擎进行了很大的改变。 Notepad++ 正则表达式不支持该栏,结果证明这是相当严重的。
为了详细说明其工作原理,我们有 3 个顶级匹配替代方案,由横线 (
|
) 分隔。第一个替代方案是:匹配 2011 年 10 月中的任何日期,且 DD 不等于 01。为了在字符级别支持 02-31,需要一个子栏组,
([0-3][2-9] |[1-3]1)
是必要的。该栏的左侧匹配 02-39,省略 11、21 和 31,右侧精确接受这些省略。下一个顶级替代项是:
匹配 2011 年 11 月和 12 月中的任意一天。
最后一组是:
匹配 2012-2019 年任意月份中的任意一天。
It probably shouldn't be done, but it can be done:
This assumes all dates are DD-MM-YYYY and valid, and that you don't need to find dates further in the future than 2019, for which it could be adapted if necessary.
Tested in Dreamweaver CS5, and I doubt they've changed their regex engine much over time. Notepad++ regex doesn't support the bar, which turned out to be rather crippling.
For a breakdown of why this works, we have 3 top level alternatives for matching, separated by the bar (
|
). The first alternative is:Which matches any dates in October 2011 with DD not equal to 01. In order to support 02-31 at the character level, a sub bar group,
([0-3][2-9]|[1-3]1)
is necessary. The left hand side of this bar matches 02-39, omitting 11, 21, and 31, and the right hand side accepts precisely those omissions.The next top level alternative is:
Which matches any day in the months of November and December of 2011.
And the final group is:
Which matches any day of any month in 2012-2019.