当前位置:网站首页>Strongest date regular expression
Strongest date regular expression
2022-04-23 10:45:00 【Glacier Technology】
One 、 Simple date judgment (YYYY/MM/DD):
^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$
Two 、 The date of evolution (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} Japan $)$
3、 ... and 、 Add leap year judgment :
example :
^((((1[6-9]|[2-9]\d)\d{2})-(0?[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 legal date range ? For different application scenarios , There are different explanations for this problem . Adopted here MSDN The agreement in :
DateTime Value class Type representation value Fan around In A.D. ( Christ Discipline element ) 0001 year 1 month 1 Midnight 12:00:00 A.D. (C.E.) 9999 year 12 month 31 Japan On the evening of On 11:59:59 And between Date and Time .
2、 The explanation of leap year .
Turn off Yu Gong Calendar Leap Year is This rule Set the : The earth Around the Sun Gong turn Once a week is called return year , Once return year Long 365 Japan 5 when 48 branch 46 second . therefore , Male Calendar rules There must be weekdays and Leap year , Every year there are 365 Japan , Bihui return Short years 0.2422 Japan , Four years is short 0.9688 Japan , so Every time Add one day in four years , this In a year 366 Japan , Just yes Leap year . But four years add one day to four returns return Years and more 0.0312 Japan ,400 There will be more 3.12 Japan , Therefore, in 400 In the middle of the year set up 3 individual Leap year , That is to say 400 Only... In the middle of the year set up 97 individual Leap year , this sample Male calendar Annual average Long And back return Years are similar . thus gauge set : The year is a must for a hundred Must be yes 400 The multiple of is Leap year , for example 1900 year 、 2100 Years are not Leap year .
The first thing you need to verification year , display however , Year fan Surrounded by 0001 - 9999 , matching YYYY Positive of be expression by :
[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}
among [0-9] It can also be expressed as \d, but \d Not as good as [0-9] intuitive , So I'll always use [0-9]
There are two difficulties in using regular expressions to verify dates : First, the number of days in big and small months is different , Second, leap year .
For the first difficulty , We don't think about leap years in the first place , hypothesis 2 Every month is 28 God , such , Month and date can be divided into three situations :
1、 Month is 1, 3, 5, 7, 8, 10, 12, The range of days is 01 - 31, matching MM-DD The regular expression of is :
(0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01])
2、 Month is 4, 6, 9, 11, The range of days is 01-30, matching MM-DD The regular expression of is :
(0[469]|11)-(0[1-9]|[12][0-9]|30)
3、 Month is 2, Consider the situation in ordinary years , matching MM-DD The regular expression of is :
02-(0[1-9]|[1][0-9]|2[0-8])
According to the above results , We can get the matching year date format as YYYY-MM-DD Regular expression of :
([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])))
Now let's solve the second difficulty : Leap year considerations . According to the definition of leap year , We can divide leap years into two categories :
1、 Can be 4 Divide but not be 100 Divisible year . Look for the change of the last two figures , You can quickly get the following regular match :
([0-9]{2})(0[48]|[2468][048]|[13579][26])
2、 Can be 400 Divisible year . Can be 400 Divisible numbers are bound to be 100 to be divisible by , So the last two must be 00, We just need to make sure that the top two can be 4 Can be divided exactly by , The corresponding regular expression is :
(0[48]|[2468][048]|[3579][26])00
2. Regular expression of the strongest validation date , Added leap year validation This date regular expression supports YYYY-MM-DD YYYY/MM/DD YYYY_MM_DD YYYY.MM.DD In the form of match : 2015-10-05 2015/10/05not match : 2015-10-05 2015-10-05
The complete regular 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)$))
Leap Year of 2 Month has 29 God , So match Leap Year date format by YYYY-MM-DD Positive of be expression by :
(([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29 Last , Merge the date validation expressions of normal year and leap year , We get the final validation date format as YYYY-MM-DD The regular expression of 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)DD/MM/YYYY The regular validation expression for the format is :(((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])00)))
版权声明
本文为[Glacier Technology]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230922150928.html
边栏推荐
猜你喜欢
Chapter 120 SQL function round
Idea - indexing or scanning files to index every time you start
Sim Api User Guide(5)
Reading integrity monitoring techniques for vision navigation systems - 3 background
【省选联考 2022 D2T1】卡牌(状态压缩 DP,FWT卷积)
Windows installs redis and sets the redis service to start automatically
解决方案架构师的小锦囊 - 架构图的 5 种类型
【leetcode】199. Right view of binary tree
Solve the problem of installing VMware after uninstalling
SQL Server 递归查询上下级
随机推荐
Shell script interaction free
得到知识服务app原型设计比较与实践
二叉树的构建和遍历
Diary of dishes | Blue Bridge Cup - hexadecimal to octal (hand torn version) with hexadecimal conversion notes
MapReduce compression
精彩回顾 | DEEPNOVA x Iceberg Meetup Online《基于Iceberg打造实时数据湖》
Ansible cloud computing automation command line compact version
What about Jerry's stack overflow? [chapter]
Example of pop-up task progress bar function based on pyqt5
Net start MySQL MySQL service is starting MySQL service failed to start. The service did not report any errors.
202. Happy number
MySql常用语句
Read integrity monitoring techniques for vision navigation systems
Arm debugging (1): two methods to redirect printf to serial port in keil
使用zerotier让异地设备组局域网
242. Valid Letter ectopic words (hash table)
微信小程序中app.js文件、组件、api
定义链表(链表)
Turn: Maugham: reading is a portable refuge
Introduction to wechat applet, development history, advantages of applet, application account, development tools, initial knowledge of wxml file and wxss file