问题
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