日期:2014-05-18 浏览次数:20452 次
将平年和闰年的日期验证表达式合并,验证日期格式为YYYY-MM-DD的正则表达式为: (([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格式的正则验证表达式为: (((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)))
------解决方案--------------------
^(?=\d)(?:(?!(?:1582(?:\.|-|\/)10(?:\.|-|\/)(?:0?[5-9]|1[0-4]))|(?:1752(?:\.|-|\/)0?9(?:\.|-|\/)(?:0?[3-9]|1[0-3])))(?=(?:(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:\d\d)(?:[02468][048]|[13579][26]))\D0?2\D29)|(?:\d{4}\D(?!(?:0?[2469]|11)\D31)(?!0?2(?:\.|-|\/)(?:29|30))))(\d{4})([-\/.])(0?\d|1[012])\2((?!00)[012]?\d|3[01])(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$
------解决方案--------------------
日期这种需要逻辑判断的东西不能用正则来处理吧?
(\d{2}|\d{4})-(\d{1,2})-(\d{1,2})( \d{2}:\d{2}:\d{2})?
------解决方案--------------------
匹配下列日期,并判断闰年,其中除年份以外都必须是两位:
yyyy
yyyy/MM
yyyy/MM/dd
yyyy/MM/dd HH
yyyy/MM/dd HH:mm
yyyy/MM/dd HH:mm:ss
^((((1[6-9]|[2-9]\d)\d{2})\/(0{1}[13578]|1[02])\/(0{1}[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})\/(0{1}[13456789]|1[012])\/(0{1}[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})\/0{1}2\/(0{1}[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{1}2\/29))$|^([1-9]{1}\d{3})$|^(([1-9]{1}\d{3})\/((0[1-9]{1})|1[12]{1}))$|^(((((1[6-9]|[2-9]\d)\d{2})\/(0{1}[13578]|1[02])\/(0{1}[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})\/(0{1}[13456789]|1[012])\/(0{1}[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})\/0{1}2\/(0{1}[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{1}2\/29)) ((20|21|22|23|[0-1]{1}\d):[0-5]{1}\d:[0-5]{1}\d)|((20|21|22|23|[0-1]{1}\d):[0-5]{1}\d)|(20|21|22|23|[0-1]{1}\d))$|^(((((((1[6-9]|[2-9]\d)\d{2})\/(0{1}[13578]|1[02])\/(0{1}[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})\/(0{1}[13456789]|1[012])\/(0{1}[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})\/0{1}2\/(0{1}[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[357