Detailed description of the regular expression of the verification date

Source: Internet
Author: User

Before entering the subject, we need to clarify two concepts:

1. What is a valid date range? This issue varies with different application scenarios. Here we adopt the conventions in MSDN:

DateTime value type indicates the date and time between midnight 12:00:00 on January 1, January 1, 0001 and 11:59:59 on January 1, December 31, 9999.

Ii. Explanation of the leap year. The interpretation of the Primary School Mathematics Office of the People's Education Press is simple and easy to understand (mediawiki and so on do not explain why the whole century must be a multiple of 400 is a leap year ):

The earth's cycle around the sun is called a year of return, and the Earth returns to the Age of 365 at 05:48:46. Therefore, there are provisions on the Gregorian calendar for the Year of the year, compared to the year of the Year of the 365 days, a total of 0.2422 days, so every four years increase one, it is a leap year. However, the increase in one day in four years is more than 0.0312 days after four years of regression, and 400 days after 3.12. Therefore, three leap years are missing in 400, that is, only 97 leap years are set in 400, in this way, the average length of the Gregorian calendar year is similar to that of the Year of regression. Therefore, it is stipulated that the year must be a multiple of 400 to a hundred years, for example, 1900 and 2100 are not a leap year.

After understanding the above two concepts, let's get started.
First, verify the year. Obviously, the year range is 0001-9999, and the regular expression matching YYYY is:

[0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3}

[0-9] can also be expressed as/d, but/d is not as intuitive as [0-9], so I will always use [0-9]

There are two difficulties in verifying a date using regular expressions: one is that the number of days in a month is different, and the other is the consideration of a leap year.
For the first difficulty, we do not consider the leap year first. Assume that the year of February is 28 days. In this way, the month and date can be divided into three situations:

1. the month is 1, 3, 5, 7, 8, 10, 12, the number of days is 01-31, the regular expression that matches the MM-DD is:

(0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])

2, the month is 4, 6, 9, 11, the number of days is 01-30, the regular expression that matches the MM-DD is:

 (0[469]|11)-(0[1-9]|[12][0-9]|30)

3, the month is 2, consider the year, match the regular expression of the MM-DD is:

02-(0[1-9]|[1][0-9]|2[0-8])

Based on the above results, we can obtain the regular expression matching the year date format as YYYY-MM-DD:

([0-9] {3} [1-9] | [0-9] {2} [1-9] [0-9] {1} | [0-9- 9] {1} [1-9] [0-9] {2} | [1-9] [0-9] {3 }) -(0 [13578] | 1 [02])-(0 [1-9] | [12] [0-9] | 3 [01]) | (0 [469] | 11)-(0 [1-9] | [12] [0-9] | 30 )) | (02-(0 [1-9] | [1] [0-9] | 2 [0-8])

Next we will solve the second difficulty: The consideration of the leap year. Based on the definition of a leap year, we can divide a leap year into two categories:

1. The year in which the product can be divisible by 4 but cannot be divisible by 100. Find the variation rules of the last two digits and quickly obtain the following regular match:

([0-9]{2})(0[48]|[2468][048]|[13579][26])

2. The year to be divisible by 400. The number that can be divisible by 400 is certainly 100, so the last two must be 00. We only need to ensure that the first two can be divisible by 4. The corresponding regular expression is:

(0[48]|[2468][048]|[3579][26])00

A leap year has 29 days in February, so the regular expression that matches the leap year date format as YYYY-MM-DD is:

([0-9] {2}) (0 [48] | [2468] [048] | [13579] [26]) | (0 [48] | [2468] [048] | [3579] [26]) 00)-02-29

Finally, combine the expression of date verification for the Year of the year, we get the final validation date format for the YYYY-MM-DD of the regular expression is:

([0-9] {3} [1-9] | [0-9] {2} [1-9] [0-9] {1} | [0 -9] {1} [1-9] [0-9] {2} | [1-9] [0-9] {3 }) -(0 [13578] | 1 [02])-(0 [1-9] | [12] [0-9] | 3 [01]) | (0 [469] | 11)-(0 [1-9] | [12] [0-9] | 30 )) | (02-(0 [1-9] | [1] [0-9] | 2 [0-8]) | ([0-9] {2}) (0 [48] | [2468] [048] | [13579] [26]) | (0 [48] | [2468] [048] | [3579] [26]) 00)-02-29)

 

Regular Expression in DD/MM/YYYY format:

(0 [1-9] | [12] [0-9] | 3 [01])/(0 [13578] | 1 [02]) | (0 [1-9] | [12] [0-9] | 30)/(0 [469] | 11 )) | (0 [1-9] | [1] [0-9] | 2 [0-8])/(02 )) /([0-9] {3} [1-9] | [0-9] {2} [1-9] [0-9] {1} | [0 -9] {1} [1-9] [0-9] {2} | [1-9] [0-9] {3 })) | (29/02/([0-9] {2}) (0 [48] | [2468] [048] | [13579] [26]) | (0 [48] | [2468] [048] | [3579] [26 )))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.