Regular expression for validating date

Rated 3.88/5 based on 715 customer reviews

I think it might help if you set an accurate expectation, as this regex does NOT, in fact, correctly validate leap years; e.g., there is no Feb.

29th in 2013, but this regex asserts that such is valid:

The source code is compiled and tested in my dev environment.

^(((((0[1-9])|(1\d)|(2[0-8]))\/((0[1-9])|(1[0-2])))|((31\/((0[13578])|(1[02])))|((29|30)\/((0[1,3-9])|(1[0-2])))))\/((20[0-9][0-9])|(19[0-9][0-9])))|((29\/02\/(19|20)(([02468][048])|([13579][26]))))$ Hi Use this following Regular Expression Details, This will support leap year also.

It doesn’t even attempt to teach how regular expressions work or how to pull them together. The aim is to demonstrate a few possibilities and try to persuade you to experiment with them if you don’t already use Regex with SQL Server.

We suggest that, if you are an ordinary mortal like Phil or I, without special powers, you should use an application such as Regex Buddy to form, edit and interpret Regular expressions. In order that people with access only to SQL Server 2000 can use the workbench, we’ll use OLE in the examples, but they are readily adapted to CLR.

Robyn and Phil start by writing a gentle introduction to using Regular expressions for validation, data cleaning and data import in TSQL, and finally end up with a routine for doing google-style searches that show the context of hits.

Let’s see some regex examples based on different requirements around date formats List dates = new Array List(); dates.add("1/1/11"); dates.add("01/01/11"); dates.add("01/01/2011"); dates.add("01/1/2011"); dates.add("1/11/2011"); dates.add("1/11/11"); dates.add("11/1/11"); String regex = "^[0-3]?

You can’t tell a regular expression to “match a number between 1 and 31”.

Rather regular expressions work character by character. [1-9]› to match 3 followed by 0 or 1, or to match 1 or 2 followed by any digit, or to match an optional 0 followed by 1 to 9. [0-9]$"; Pattern pattern = Pattern.compile(regex); for(String date : dates) Output: 1/1/11 : true 01/01/11 : true 01/01/2011 : true 01/1/2011 : true 1/11/2011 : true 1/11/11 : true 11/1/11 : true List dates = new Array List(); //With leading zeros dates.add("01/01/11"); dates.add("01/01/2011"); //Missing leading zeros dates.add("1/1/11"); dates.add("01/1/2011"); dates.add("1/11/2011"); dates.add("1/11/11"); dates.add("11/1/11"); String regex = "^[0-3][0-9]/[0-3][0-9]/(?

The functionality of Enumeration and the Iterator are same.

You can get remove() from Iterator to remove an element, while while Enumeration does not have remove() method.

Leave a Reply