Regular expressions matching yyyy MM DD date format

Time:2021-1-15

In fact, I also have a headache about regular expressions. Baidu has collected some information. To sum up, let’s leave a backup here.

Questions to consider: what is the legal date; the number of days in each month is different; the problem of leap year….

1. Legal date: the date and time between 12:00:00 midnight on January 1, 0001 A.D. and 11:59:59 p.m. on December 31, 9999 A.D

see http://msdn.microsoft.com/zh-cn/library/system.datetime (VS.80).aspx

2. The concept of leap year: explanation of Baidu Encyclopedia

see http://baike.baidu.com/view/29649.htm

3. Integration expression

Average year:

Year can be written uniformly: (?! 0000) [0-9] {4}
The months of all years, including ordinary years, include 1-28 days: (0 [1-9] | 1 [0-2]) – (0 [1-9] | 1 [0-9] | 2 [0-8])
29 and 30 days are included in all years including ordinary year except February: (0 [13-9] | 1 [0-2]) – (29-30)
January, March, may, July, August, October and December of all years including the normal year include 31 days: (0 [13578] | 1 [02]) – 31)
All dates except February 29 of leap year: (?! 0000) [0-9] {4} – ((0 [1-9] | 1 [0-2]) – (0 [1-9] | 1 [0-9] | 2 [0-8]) – (0 [13-9] | 1 [0-2]) – (29 | 30) | (0 [13578] | 1 [02]) – 31)
Runnian

Years divisible by 4 but not by 100: ([0-9] {2} (0 [48] | [2468] [048] | [13579] [26]))
Years divisible by 400. The number divisible by 400 is definitely divisible by 100, so the last two digits must be 00: (0 [48] | [2468] [048] | [13579] [26]) 00
All in all, it is February 29 of all leap years: ([0-9] {2} (0 [48] | [2468] [048] | [13579] [26]) (0 [48] | [2468] [048] | [13579] [26]) 00) – 02-29)
The four rules have been implemented, and have no influence on each other. Together, they are the regularization of all dates in the datetime range
^((?!0000)[0-9]{4}-((0[1-9]|1[0-2])-(0[1-9]|1[0-9]|2[0-8])|(0[13-9]|1[0-2])-(29|30)|(0[13578]|1[02])-31)|([0-9]{2}(0[48]|[2468][048]|[13579][26])|(0[48]|[2468][048]|[13579][26])00)-02-29)$

Considering that the regular expression is only used for verification, the capture group is meaningless and will only occupy resources and affect the matching efficiency, so the non capture group can be used for optimization.
^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$

For capture / non capture of regular expressions, see: https://www.jb51.net/article/28035.htm

Finally, the updated JavaScript method

Copy codeThe code is as follows:
function checkDate(fname){
var sc = $(“#”+fname);
var s = sc.val();
if (sc==null){
alert(“Element is null”);
return true;
}
var reg=/^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/;
if(!s.match(reg)){
alert(“false”);
}else{
alert(“true”); }

}

Copy codeThe code is as follows:
<body>
<input type=”date” name=”textfield” >
< input type = button “value = button” onclick = checkdate (‘text1 ‘) “>
</body>

Correct regular expression (including test code)

Copy codeThe code is as follows:
var str = ‘2009-12-33’;
if(str.match(/^((?:19|20)\d\d)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])$/)) {
Alert (‘Is the date ‘);
} else {
Alert (‘not a date ‘);
}

Recommended Today

Socket communication between Java and python

Code service code, call the corresponding service according to the code Status code, return the status code according to the execution result Using JSON string communication between sockets Java socket client and python socket server Java socket client package com.brewin.hammer.system.util.socket; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintWriter; import java.net.Socket; import java.util.HashMap; import java.util.Map; import com.alibaba.fastjson.JSON; /** […]