大于日期格式 xx-xx-xxxx 的正则表达式

发布于 2025-01-05 23:24:02 字数 91 浏览 5 评论 0原文

我有一个包含 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眉目亦如画i 2025-01-12 23:24:02

它可能不应该这样做,但可以这样做:

([0-3][2-9]|[1-3]1)-10-2011|[0-3][0-9]-1[12]-2011|[0-3][0-9]-[01][0-9]-201[2-9]

假设所有日期都是 DD-MM-YYYY 并且有效,并且您不需要查找 2019 年以后的日期,因此可以对其进行调整如果需要的话。

在 Dreamweaver CS5 中进行了测试,我怀疑他们随着时间的推移已经对正则表达式引擎进行了很大的改变。 Notepad++ 正则表达式不支持该栏,结果证明这是相当严重的。

为了详细说明其工作原理,我们有 3 个顶级匹配替代方案,由横线 (|) 分隔。第一个替代方案是:

 ([0-3][2-9]|[1-3][0-9])-10-2011

匹配 2011 年 10 月中的任何日期,且 DD 不等于 01。为了在字符级别支持 02-31,需要一个子栏组,([0-3][2-9] |[1-3]1) 是必要的。该栏的左侧匹配 02-39,省略 11、21 和 31,右侧精确接受这些省略。

下一个顶级替代项是:

[0-3][0-9]-1[12]-2011

匹配 2011 年 11 月和 12 月中的任意一天。

最后一组是:

[0-3][0-9]-[01][0-9]-201[2-9]

匹配 2012-2019 年任意月份中的任意一天。

It probably shouldn't be done, but it can be done:

([0-3][2-9]|[1-3]1)-10-2011|[0-3][0-9]-1[12]-2011|[0-3][0-9]-[01][0-9]-201[2-9]

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:

 ([0-3][2-9]|[1-3][0-9])-10-2011

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:

[0-3][0-9]-1[12]-2011

Which matches any day in the months of November and December of 2011.

And the final group is:

[0-3][0-9]-[01][0-9]-201[2-9]

Which matches any day of any month in 2012-2019.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文