我需要用正则表达式验证格式为dd/mm/yyyy的日期字符串。
这个正则表达式验证dd/mm/yyyy,但不验证像31/02/4500这样的无效日期:
^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$
什么是有效的正则表达式来验证支持闰年的dd/mm/yyyy格式?
我需要用正则表达式验证格式为dd/mm/yyyy的日期字符串。
这个正则表达式验证dd/mm/yyyy,但不验证像31/02/4500这样的无效日期:
^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$
什么是有效的正则表达式来验证支持闰年的dd/mm/yyyy格式?
当前回答
这里我为dd/mm/yyyy写了一个,其中分隔符可以是-之一。,/年范围0000-9999。
它处理闰年,是为正则表达式风格设计的,支持查找头,捕获组和反向引用。d/m/yyyy等无效。如果需要,在[-.,/]中添加分隔符
^(?=\d{2}([-.,\/])\d{2}\1\d{4}$)(?:0[1-9]|1\d|[2][0-8]|29(?!.02.(?!(?!(?:[02468][1-35-79]|[13579][0-13-57-9])00)\d{2}(?:[02468][048]|[13579][26])))|30(?!.02)|31(?=.(?:0[13578]|10|12))).(?:0[1-9]|1[012]).\d{4}$
测试regex101;作为Java字符串:
"^(?=\\d{2}([-.,\\/])\\d{2}\\1\\d{4}$)(?:0[1-9]|1\\d|[2][0-8]|29(?!.02.(?!(?!(?:[02468][1-35-79]|[13579][0-13-57-9])00)\\d{2}(?:[02468][048]|[13579][26])))|30(?!.02)|31(?=.(?:0[13578]|10|12))).(?:0[1-9]|1[012]).\\d{4}$"
解释道:
(?x) # modifier x: free spacing mode (for comments)
# verify date dd/mm/yyyy; possible separators: -.,/
# valid year range: 0000-9999
^ # start anchor
# precheck xx-xx-xxxx,... add new separators here
(?=\d{2}([-.,\/])\d{2}\1\d{4}$)
(?: # day-check: non caturing group
# days 01-28
0[1-9]|1\d|[2][0-8]|
# february 29d check for leap year: all 4y / 00 years: only each 400
# 0400,0800,1200,1600,2000,...
29
(?!.02. # not if feb: if not ...
(?!
# 00 years: exclude !0 %400 years
(?!(?:[02468][1-35-79]|[13579][0-13-57-9])00)
# 00,04,08,12,...
\d{2}(?:[02468][048]|[13579][26])
)
)|
# d30 negative lookahead: february cannot have 30 days
30(?!.02)|
# d31 positive lookahead: month up to 31 days
31(?=.(?:0[13578]|10|12))
) # eof day-check
# month 01-12
.(?:0[1-9]|1[012])
# year 0000-9999
.\d{4}
$ # end anchor
参见SO正则表达式常见问题解答;如果失败了,请告诉我。
其他回答
对于日期MM/DD/YYYY,可以使用
^((((0[13578])|([13578])|(1[02]))[\/](([1-9])|([0-2][0-9])|(3[01])))|(((0[469])|([469])|(11))[\/](([1-9])|([0-2][0-9])|(30)))|((2|02)[\/](([1-9])|([0-2][0-9]))))[\/]\d{4}$|^\d{4}$
它可以验证正确的日期和飞蛾。
请记住,您可以检查正则表达式at
regex101
我推荐这个:)
玩得开心!
我知道这是一个切题的答案,但如果问题的意图是“我如何验证日期?”,那么为什么不试着让编程语言来完成所有的艰苦工作(如果你正在使用一种语言的话)?
例如在PHP中
$this_date_object = date_create($this_date);
if ($this_date_object == false )
{
// process the error
}
我怀疑,在不知道用户的地区何时从儒略历切换到格里高利历的情况下,以下内容是尽可能准确的。
它接受'-','/',或者什么都不作为年,月和日之间的分隔符,不管顺序如何。
MMddyyyy:
^(((0[13-9]|1[012])[-/]?(0[1-9]|[12][0-9]|30)|(0[13578]|1[02])[-/]?31|02[-/]?(0[1-9]|1[0-9]|2[0-8]))[-/]?[0-9]{4}|02[-/]?29[-/]?([0-9]{2}(([2468][048]|[02468][48])|[13579][26])|([13579][26]|[02468][048]|0[0-9]|1[0-6])00))$
ddMMyyyy:
^(((0[1-9]|[12][0-9]|30)[-/]?(0[13-9]|1[012])|31[-/]?(0[13578]|1[02])|(0[1-9]|1[0-9]|2[0-8])[-/]?02)[-/]?[0-9]{4}|29[-/]?02[-/]?([0-9]{2}(([2468][048]|[02468][48])|[13579][26])|([13579][26]|[02468][048]|0[0-9]|1[0-6])00))$
yyyyMMdd:
^([0-9]{4}[-/]?((0[13-9]|1[012])[-/]?(0[1-9]|[12][0-9]|30)|(0[13578]|1[02])[-/]?31|02[-/]?(0[1-9]|1[0-9]|2[0-8]))|([0-9]{2}(([2468][048]|[02468][48])|[13579][26])|([13579][26]|[02468][048]|0[0-9]|1[0-6])00)[-/]?02[-/]?29)$
除了顺序,这些都精确到儒略历(每四年闰年),直到1700年,当公历与儒略历背离。它有两个问题:
It accepts the year 0000, which doesn't exist in many, but not all, standards. Note that ISO 8601 does accept year 0000 (equivalent to 1 BCE). It doesn't skip the 10-13 days which were lost when the Gregorian Calendar came into use. This varies by locality though. For example, the Roman Catholic Church skipped 10 days, October 5th through October 14th, 1582, but Greece (the last to switch) skipped February 16th through the 28th of 1923, 13 days, having to take into account the leap years of 1700, 1800, and 1900.
从0001年到9999年的Java日历实现已经测试了这一点,唯一的差异是上面提到的1582年的10天。
仅限当天使用:
<input placeholder="day" maxlength="2" minlength="1" formControlName="birthDay"
name="birthDay"pattern="(0[1-9]|1[0-9]|2[0-9]|3[0-1])" >/
仅供当月使用:
<input placeholder="month" maxlength="2" minlength="1"
formControlName="month" name="month" formControlName="month" name="month" pattern="(0[1-
9]|1[0-2])">/
这是一个正则表达式,用于匹配日期格式的字符串,YYYY-MM-DD,使用不同类型的分隔符。它甚至在句子中匹配字符串,并且日期以st, nd和其他结尾。
举个例子,它与下面句子中的日期相匹配:
"Her birthday is 2022 February 23rd; I will present here a gift."
"Her birthday is 2022 Feb 23rd; I will present here a gift."
"Her birthday is 2022 02 23; I will present here a gift."
这是日期正则表达式:
"\b
(?<YYYY>[0-9]{4})
(?<!0000)(?<sep>[ /.,-])
(?|
(?:(?<MM>0[13578]|1[02]|Jan(?:uary)?|Mar(?:ch)?|May|Jul(?:y)?|Aug(?:ust)?|Oct(?:ober)?|Dec(?:ember)?)\g{sep}(?<DD>0[1-9]|[12][0-9]|3[01]))|
(?:(?<MM>0[469]|11|Apr(?:il)?|Jun(?:e)?|Sep(?:tember)?|Nov(?:ember)?)\g{sep}(?<DD>0[1-9]|[12][0-9]|30))|
(?:(?<MM>02|Feb(?:ruary)?)\g{sep}(?<DD>0[1-9]|[12][0-9]))
)
(?:
(?<=[023][1])st|
(?<=[02][2])nd|
(?<=[02][3])rd|
(?<=(?:0[4-9])|(?:1[0-9])|20|(?:2[4-9])|30)th
)?
[,;.]?
\b"x
以我之见,对于正则表达式来说,检查闰年是没有意义的,因为可以为它编写简单、清晰和可理解的一行代码:
is_leap_year(y) = ((y%4 == 0) && (y%100 != 0)) || (y%400 == 0)
正则表达式是用来匹配字符串的,而不是用来进行计算的。最好的方法是匹配字符串,然后将捕获的MM组传递给is_leap_year函数(如果是02、Feb或February),以验证字符串。