我是靠谱客的博主 单薄火车,最近开发中收集的这篇文章主要介绍正则表达式匹配日期时间,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

正则表达式(?n:^(?=d)((?<day>31(?!(.0?[2469]|11))|30(?!.0?2)|29(?(.0?2)(?=.{3,4}(1[6-9]|[2-9]d)(0[48]|[2468][048]|[13579][26])|(16|[2468][048]|[3579][26])00))|0?[1-9]|1d|2[0-8])(?<sep>[/.-])(?<month>0?[1-9]|1[012])2(?<year>(1[6-9]|[2-9]d)d{2})(?:(?=x20d)x20|$))?(?<time>((0?[1-9]|1[012])(:[0-5]d){0,2}(?i: [AP]M))|([01]d|2[0-3])(:[0-5]d){1,2})?$)
匹配31/12/2003 | 29/2/2004 4:50 PM | 23:59:59
不匹配12/31/2003 | 29/2/2003 | 4:00

正则表达式^(?:(?:31(/|-|.)(?:0?[13578]|1[02]))1|(?:(?:29|30)(/|-|.)(?:0?[1,3-9]|1[0-2])2))(?:(?:1[6-9]|[2-9]d)?d{2})$|^(?:29(/|-|.)0?23(?:(?:(?:1[6-9]|[2-9]d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1d|2[0-8])(/|-|.)(?:(?:0?[1-9])|(?:1[0-2]))4(?:(?:1[6-9]|[2-9]d)?d{2})$
匹配29/02/1972 | 5-9-98 | 10-11-2002
不匹配29/02/2003 | 12/13/2002 | 1-1-1500

正则表达式^(?:(((Jan(uary)?|Ma(r(ch)?|y)|Jul(y)?|Aug(ust)?|Oct(ober)?|Dec(ember)?) 31)|((Jan(uary)?|Ma(r(ch)?|y)|Apr(il)?|Ju((ly?)|(ne?))|Aug(ust)?|Oct(ober)?|(Sept|Nov|Dec)(ember)?) (0?[1-9]|([12]d)|30))|(Feb(ruary)? (0?[1-9]|1d|2[0-8]|(29(?=, ((1[6-9]|[2-9]d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))))))), ((1[6-9]|[2-9]d)d{2}))
匹配Jan 1, 2003 | February 29, 2004 | November 02, 3202
不匹配Feb 29, 2003 | Apr 31, 1978 | jan 33,3333

正则表达式^(([0-1]?[0-9])|([2][0-3])):([0-5]?[0-9])(:([0-5]?[0-9]))?$
匹配12:15 | 10:26:59 | 22:01:15
不匹配24:10:25 | 13:2:60

正则表达式(?=d)^(?:(?!(?:10D(?:0?[5-9]|1[0-4])D(?:1582))|(?:0?9D(?:0?[3-9]|1[0-3])D(?:1752)))((?:0?[13578]|1[02])|(?:0?[469]|11)(?!/31)(?!-31)(?!.31)|(?:0?2(?=.?(?:(?:29.(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:dd)(?:[02468][048]|[13579][26])(?!x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)x20BC))))))|(?:0?2(?=.(?:(?:dD)|(?:[01]d)|(?:2[0-8])))))([-./])(0?[1-9]|[12]d|3[01])2(?!0000)((?=(?:00(?:4[0-5]|[0-3]?d)x20BC)|(?:d{4}(?!x20BC)))d{4}(?:x20BC)?)(?:$|(?=x20d)x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]d){0,2}(?:x20[aApP][mM]))|(?:[01]d|2[0-3])(?::[0-5]d){1,2})?$
匹配11/24/0004 11:59 PM | 2.29.2008 | 02:50:10
不匹配12/33/1020 | 2/29/2005 | 13:00 AM

正则表达式^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$
匹配10/03/1979 | 1-1-02 | 01.1.2003
不匹配10/03/197 | 09--02--2004 | 01 02 03

正则表达式^((0?[13578]|10|12)(-|/)(([1-9])|(0[1-9])|([12])([0-9]?)|(3[01]?))(-|/)((19)([2-9])(d{1})|(20)([01])(d{1})|([8901])(d{1}))|(0?[2469]|11)(-|/)(([1-9])|(0[1-9])|([12])([0-9]?)|(3[0]?))(-|/)((19)([2-9])(d{1})|(20)([01])(d{1})|([8901])(d{1})))$
匹配1/2/03 | 02/30/1999 | 3/04/00
不匹配3/4/2020 | 3/4/1919 | 4/31/2000

正则表达式^([2-9]d{3}((0[1-9]|1[012])(0[1-9]|1d|2[0-8])|(0[13456789]|1[012])(29|30)|(0[13578]|1[02])31)|(([2-9]d)(0[48]|[2468][048]|[13579][26])|(([2468][048]|[3579][26])00))0229)$
匹配20000101 | 20051231 | 20040229
不匹配19990101 | 20053112 | 20050229

正则表达式^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9][aApP][mM]){1}$
匹配08:00AM | 10:00am | 7:00pm
不匹配13:00pm | 12:65am

正则表达式^(([1-9])|(0[1-9])|(1[0-2]))/(([0-9])|([0-2][0-9])|(3[0-1]))/(([0-9][0-9])|([1-2][0,9][0-9][0-9]))$
匹配01/01/2001 | 1/1/1999 | 10/20/2080
不匹配13/01/2001 | 1/1/1800 | 10/32/2080

正则表达式^d{1,2}/d{1,2}/d{4}$
匹配4/1/2001 | 12/12/2001 | 55/5/3434
不匹配1/1/01 | 12 Jan 01 | 1-1-2001

正则表达式(d{2}|d{4})(?:-)?([0]{1}d{1}|[1]{1}[0-2]{1})(?:-)?([0-2]{1}d{1}|[3]{1}[0-1]{1})(?:s)?([0-1]{1}d{1}|[2]{1}[0-3]{1})(?::)?([0-5]{1}d{1})(?::)?([0-5]{1}d{1})
匹配00-00-00 00:00:00 | 0000-00-00 00:00:00 | 09-05-22 08:16:00 | 1970-00-00 00:00:00 | 20090522081600
不匹配2009-13:01 00:00:00 | 2009-12-32 00:00:00 | 2002-12-31 24:00:00 | 2002-12-31 23:60:00 | 02-12-31 23:00:60

正则表达式^(?=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))(?:(?:dd)(?:[02468][048]|[13579][26]))D0?2D29)|(?:d{4}D(?!(?:0?[2469]|11)D31)(?!0?2(?:.|-|/)(?:29|30))))(d{4})([-/.])(0?d|1[012])2((?!00)[012]?d|3[01])(?:$|(?=x20d)x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]d){0,2}(?:x20[aApP][mM]))|(?:[01]d|2[0-3])(?::[0-5]d){1,2})?$
匹配0008-02-29 | 2:34:59 PM | 9999/12/31 11:59 PM
不匹配04/04/04 | 1:00 | 1999/1/32

正则表达式^(([1-9]{1})|([0-1][0-9])|([1-2][0-3])):([0-5][0-9])$
匹配00:00 | 23:59 | 10:10
不匹配24:00 | 00:60 | 25:61

正则表达式^[0-9]{4}-(((0[13578]|(10|12))-(0[1-9]|[1-2][0-9]|3[0-1]))|(02-(0[1-9]|[1-2][0-9]))|((0[469]|11)-(0[1-9]|[1-2][0-9]|30)))$
匹配2004-04-30 | 2004-02-29
不匹配2004-04-31 | 2004-02-30


转自:http://see.xidian.edu.cn/cpp/html/1434.html

最后

以上就是单薄火车为你收集整理的正则表达式匹配日期时间的全部内容,希望文章能够帮你解决正则表达式匹配日期时间所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(39)

评论列表共有 0 条评论

立即
投稿
返回
顶部