Regular Expression for greater than date format xx-xx-xxxx

亡梦爱人 提交于 2019-12-12 00:37:21

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/9318402/regular-expression-for-greater-than-date-format-xx-xx-xxxx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!