Year, month, and day-Regular Expression

Source: Internet
Author: User

PS: After learning Regular Expressions for so long, I didn't make any decent patterns. I woke up early and came up with a whimsy, I made up my mind to prove that my regular expression is not white-so I chose the most common date match ......

 

Date Format: year-month-day, that is, yyyy-mm-dd. If today's date is 2008-11-01, of course, the yyyy-mm-dd format is-11-01. Our expression should match the two types of dates at the same time-when the month and day are single digits, the '0' on the front can be dispensable.

 

Another point is that there are not many opportunities for us to match the Perpetual calendar. In general, the starting and ending dates are enough to match the birthdate, so my expression only matches -- it is estimated that it is difficult for me to surpass these two years in general ......

 

Start to write expressions.

 

First of all, it is the year --/(19 | 20) \ D {2 })/. (Perl is used here. Because JavaScript follows this method, and this method is really simple, I personally like it very much .)

 

Matching a year is the simplest part of the entire expression. The following statements for setting up a month or a day may have many situations. I can only set multiple modes for matching in the entire expression, in these models, the year is basically the same (except for a leap year ).

 

One thing primary school students know: in the 12 months of a year, there are 31 days each month in March, there are 30 days each month in March 4, 6, 9, and, and the most special one in March is that there are only 28 days in March and 29 days in a leap year-for a leap year, we will consider special processing after processing.

 

31 days month --/(0? [2, 13578] | 1 [02])/.

Date --/(0? [1-9] | [12] \ d | 3 [01])/.

1. In summary, The 31-day month pattern is:/(year)-(month)-(day)/, that is,/(19 | 20) \ D {2 }) -(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01])/

 

30 days month --/(0? [469] | 11 )/.

Date --/(0? [1-9] | [12] \ d | 30 )/.

2. In summary,The month pattern for 30 days is:/(year)-(month)-(day)/, that is,/(19 | 20) \ D {2})-(0? [469] | 11)-(0? [1-9] | [12] \ d | 30 )/

 

The following matches the month of the general year, that is, the month of the 28-Day.

Month --/0? 2/

Date --/(0? [1-9] | 1 \ d | 2 [0-8])/

3. In summary, The 28-day February pattern is:/(year)-(month)-(day)/, that is,/(19 | 20) \ D {2})-0? 2-(0? [1-9] | 1 \ d | 2 [0-8])/

 

Integration 1, 2, 3, You can create a date that matches except the leap year, that is --
/(31-day month) | (30-day month) | (February, 28 days)/, that is --
/(19 | 20) \ D {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01]) | (19 | 20) \ D {2})-(0? [469] | 11)-(0? [1-9] | [12] \ d | 30) | (19 | 20) \ D {2})-0? 2-(0? [1-9] | 1 \ d | 2 [0-8])/

 

In this step, we will begin to consider the year February of the leap year. We all know that the year February has 29 days, but what is the leap year ??

Let's take a moment to list the 1900-2099, which year is a leap year.

, 2064,2068, 2072,2076, 2080,2084, 2088,2092, 2096

 

Leap year:If it can be divided by four, it must be divided by 400 for the whole century. For example, 1900 is a century but cannot be divided by 400, so it is not a leap year, and 2000 is a leap year.

 

Maybe we all know how to calculate a leap year, but the regular expression does not know, and the regular expression does not have the ability to calculate internally, what we can do is to find a solution that can list all possible solutions within a limited number of times. Therefore, I listed all the leap years above, instead, we want to find the rule for the expression ......

 

After carefully analyzing the leap year, it is easy to find:
1.The single digit must be an even number.
2.When the number of digits is an odd number, there are only two possible single digits: 2 and 6.
3.When the number of digits is an even number (except 0), there are three possible single digits: 0, 4, and 8.
4.When the number of digits is 0, the number of single digits is 4 or.

 

Therefore, the year of a leap year is matched as follows:/(19 | 20) ([13579] [26] | [2468] [048] | 0 [48]) | (2000 ))/

Therefore, the matching mode for the leap year February is/(19 | 20) ([13579] [26] | [2468] [048] | 0 [48]). | (2000)-0? 2-(0? [1-9] | [12] \ D )/

 

Combining all previous modelsTo obtain the regular expression that matches the date.
/^ (19 | 20) \ D {2})-(0? [1, 13578] | 1 [02])-(0? [1-9] | [12] \ d | 3 [01]) | (19 | 20) \ D {2})-(0? [469] | 11)-(0? [1-9] | [12] \ d | 30) | (19 | 20) \ D {2})-0? 2-(0? [1-9] | 1 \ d | 2 [0-8]) | (19 | 20) ([13579] [26] | [2468] [048] | 0 [48]) | (2000)-0? 2-(0? [1-9] | [12] \ D) $/

 

The above expressions are not optimal. At least the following methods will be better:
/^ (19 | 20) \ D {2})-(0? [13-9] | 1 [012])-(0? [1-9] | [12] \ d | 30) | (19 | 20) \ D {2})-(0? [13578] | 1 [02])-31) | (19 | 20) \ D {2})-0? 2-(0? [1-9] | 1 \ d | 2 [0-8]) | (19 | 20) ([13579] [26] | [2468] [048] | 0 [48]) | (2000)-0? 2-29) $/
Matching rules of the expression (in the order of sequence below ):
1.Match 1-30 days except January
2.If 1 does not match, 1 matches the 31 days of, and January.
3.If 2 cannot be matched, it matches the day 1-28 of January.

Year, month, and day-Regular Expression

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.