The regular expressions provided in this article, which are used to validate the code for a leap year, were previously written for date validation, but unified, including all dates, now we specialize in providing a date validation program for a leap year.
1: Four Years a leap
([0-9]{2} (0[48]|[ 2468][048]| [13579] [26])
2: Hundred years does not leap, 400 year again leap
(0[48]| [2468] [048]| [13579] [26]) 00
3: Together is the February 29 of all leap years
([0-9]{2} (0[48]|[ 2468][048]| [13579] [26]) | (0[48]| [2468] [048]| [13579] [26]) 00)-02-29)
All four rules are implemented and have no effect on each other, and together is the regular of all dates that match 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) $
Given that this regular expression is used only as validation, capturing groups are meaningless, consume resources and affect the efficiency of matching, so you can use a non-capturing group 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) $