Date Regular Expression posts)

Source: Internet
Author: User
1. Simple date judgment (yyyy/mm/DD ): ^ \ D {4} (\-| \/| \.) \ D {1, 2} \ 1 \ D {1, 2} $ Ii. Evolutionary date judgment (yyyy/mm/DD | YY/MM/DD ):   ^ (\ D {4} | \ D {2}) (\-| \/| \.) \ D {1, 2} \ 3 \ D {1, 2} $) | (^ \ D {4} Year \ D {1, 2} Month \ D {1, 2} Day $) $ Iii. instance: ^ (1 [6-9] | [2-9] \ D) \ D {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01]) | (1 [6-9] | [2-9] \ D) \ D {2})-(0? [13456789] | 1 [012])-(0? [1-9] | [12] \ d | 30) | (1 [6-9] | [2-9] \ D) \ D {2 }) -0? 2-(0? [1-9] | 1 \ d | 2 [0-8]) | (1 [6-9] | [2-9] \ D) (0 [48] | [2468] [048] | [13579] [26]) | (16 | [2468] [048] | [3579] [26]) 00)-0? 2-29-) $ Analysis: 1. What is a valid date range? This issue varies with different application scenarios. Here we adopt the conventions in msdn: Datetime Value class Type Representation Value Fan Perimeter In AD (Christ Ji RMB) 0001 Year 1 Month 1 Midnight 12:00:00 To ad (C. e) 9999 Year 12 Month 31 Day Late Upper 11:59:59 Of Room Date and Time . 2. Explanation of a leap year. Off Yu Gong Calendars Year is This rule Fixed: Earth Bypass SUN Gong Transfer Once a week Gui Year, once Gui Year Long 365 Day 5 Hour 48 Minute 46 Seconds. Therefore Calendar rules There must be a normal year and Bytes Year, year 365 Day, than back Gui Short year 0.2422 Every four years. 0.9688 , Hence Each Increase by one day in four years, This One year 366 Yes Bytes Year. But four more times a day than four times Gui More than a year 0.0312 Day , 400 More in years 3.12 Day , Therefore 400 Less than a year Set 3 Items Bytes Year , That is, in 400 Only Set 97 Items Bytes Year, This Sample Public Calendar Annual average Long Degrees and replies Gui Year is similar. Therefore Rules Set: The year is mandatory for a hundred Required Yes 400 Is a multiple Bytes Year, such 1900 Year, 2100 Year is not Bytes Year . First, you must Verify Year, Display However, the year Perimeter: 0001-9999 , Match Yyyy Of Then 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-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 [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 [2, 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 2. Most Strong Verification Positive date Then Expression, Added Bytes Years Verify

This date Regular Expression supports
YYYY-MM-DD
Yyyy/mm/dd
Yyyy_mm_dd
Yyyy. Mm. dd format

Match:20082-29 2008/02/29

Not match: 2008-2-30 2007-2-29 Complete Then The expression is as follows:: (^ (1 [8-9] \ D {2}) | ([2-9] \ D {3 }))([-\/\. _]) (10 | 12 | 0? [13578]) ([-\/\. _]) (3 [01] | [12] [0-9] | 0? [1-9]) $) | (^ (1 [8-9] \ D {2}) | ([2-9] \ D {3 })) ([-\/\. _]) (11 | 0? [469]) ([-\/\. _]) (30 | [12] [0-9] | 0? [1-9]) $) | (^ (1 [8-9] \ D {2}) | ([2-9] \ D {3 })) ([-\/\. _]) (0? 2) ([-\/\. _]) (2 [0-8] | 1 [0-9] | 0? [1-9]) $) | (^ ([2468] [048] 00) ([-\/\. _]) (0? 2 )([-\/\. _]) (29) $) | (^ ([3579] [26] 00 )([-\/\. _]) (0? 2 )([-\/\. _]) (29) $) | (^ ([1] [89] [0] [48]) ([-\/\. _]) (0? 2 )([-\/\. _]) (29) $) | (^ ([2-9] [0-9] [0] [48]) ([-\/\. _]) (0? 2 )([-\/\. _]) (29) $) | (^ ([1] [89] [2468] [048]) ([-\/\. _]) (0? 2 )([-\/\. _]) (29) $) | (^ ([2-9] [0-9] [2468] [048]) ([-\/\. _]) (0? 2 )([-\/\. _]) (29) $) | (^ ([1] [89] [13579] [26]) ([-\/\. _]) (0? 2 )([-\/\. _]) (29) $) | (^ ([2-9] [0-9] [13579] [26]) ([-\/\. _]) (0? 2) ([-\/\. _]) (29) $ )) Bytes Years 2 Month has 29 Days, so match Bytes Year date format Is YYYY-MM-DD Of Then Expression Is :

([0-9] {2}) (0 [48] | [2468] [048] | [13579] [26]) | (0 [48] | [2468] [048] | [3579] [26]) 00)-02-29 Finally, merge the date verification expressions for the Year of the same period with the year of the second month, we get the final validation date format as the regular expression of the YYYY-MM-DD:

([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.