当前位置:网站首页>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
边栏推荐
- Deploy jar package
- Arbitrary file reading vulnerability exploitation Guide
- 【省选联考 2022 D2T1】卡牌(状态压缩 DP,FWT卷积)
- 微信小程序简介、发展史、小程序的优点、申请账号、开发工具、初识wxml文件和wxss文件
- Xshell+Xftp 下载安装步骤
- 【leetcode】107. Sequence traversal of binary tree II
- Solution architect's small bag - 5 types of architecture diagrams
- 域名和IP地址的联系
- Comparison and practice of prototype design of knowledge service app
- 二叉树的构建和遍历
猜你喜欢
How can swagger2 custom parameter annotations not be displayed
net start mysql MySQL 服务正在启动 . MySQL 服务无法启动。 服务没有报告任何错误。
IDEA——》每次启动都会Indexing或 scanning files to index
Shell script interaction free
Solution architect's small bag - 5 types of architecture diagrams
SSH利用私钥无密钥连接服务器踩坑实录
Jerry's more accurate determination of abnormal address [chapter]
Download and installation steps of xshell + xftp
/Can etc / shadow be cracked?
Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP
随机推荐
142. Circular linked list||
SQL Server recursive query of superior and subordinate
CentOS/Linux安装MySQL
Initial exploration of NVIDIA's latest 3D reconstruction technology instant NGP
JVM——》常用参数
UEditor之——图片上传组件大小4M的限制
202. Happy number
Image processing - Noise notes
SSH uses private key to connect to server without key
MySQL how to merge the same data in the same table
Ansible playbook syntax and format automate cloud computing
Turn: Maugham: reading is a portable refuge
JVM——》常用命令
景联文科技—专业数据标注公司和智能数据标注平台
SQL Server 递归查询上下级
Using multithreading to output abc10 times in sequence
App. In wechat applet JS files, components, APIs
定义链表(链表)
SQLServer 查询数据库死锁
Read LSTM (long short term memory)