|
Manipulate Text With Regular Expressions
Determine whether a given date is greater than or equal to a predefined date, and learn how to add to the list of Standard Expressions in the Regular Expression Editor.
by Bill Wagner
Posted April 9, 2004
Technology Toolbox: VB.NET, C#, ASP.NET
Q:
Advanced Text Processing With RegEx
Is it possible to check whether a given date, in DD-MM-YYYY format, is greater than or equal to a predefined date (say 25-09-2003) using regular expressions?
A:
Regular expressions are a powerful tool for manipulating textual information, allowing you to perform the sophisticated processing you describe. Before I explain the solution, let's back up one step and discuss why you would use Regex for this problem. You would think that it would be much easier to create a System.DateTime structure with the date requested, then use System.DateTime.Compare() to determine if the given date is greater than your predefined limit. In fact, that is how I would solve this problem in almost all cases. The one case where it's worth the extra work to use regular expressions is when you want to perform the validation client-side in a Web application. ECMAScript supports regular expressions, which means you can push regular expression validation to the client. (The simpler-to-write System.DateTime comparison must take place at the server.)
Here's how it works. The regular expression language processes text input by reading the string and comparing it against a matching sequence. Let's build the regular expression that answers your question; we'll start by matching any date between 01/01/1990 and 31/12/2099. This regular expression performs that test:
(0[1-9]|[12][0-9]|3[01])[-\./]
(0[1-9]|1[012])[- /\.](19|20)\d\d
The expression is fairly easy to understand once you split it into sections. (0[1-9]|[12][0-9]|3[01]) matches a day of the month, expressed as a two-digit number: 01 to 31. 0[1-9] matches 01, 02 … 09. It matches a zero, followed by any digit from one to nine. [12][0-9] matches any number from 11 to 29. The first character must be a one or a two, followed by any character from one to nine. 3[01] matches 30 or 31. If you OR all three of them using the vertical bar, you get every number from 0 to 31, expressed with two digits.
I added one small feature from your request above. [-\./] matches any of the three characters I allowed as separators: (/), (.), or (-). The period is significant in regular expressions, so it must be escaped with the backslash.
The month portion uses the same approach: (0[1-9]|1[012]) matches any two-digit number from 01 to 12. The year portion matches any four-digit number between 1900 and 2099. (Remember that regular expressions process text; you cannot process numbers as integers.) The year section, (19|20)\d\d, matches either 19 or 20, followed by two digits.
Back to top
|