我看到这些模式中存在许多复杂化。您的日期格式实际上非常简单。我认为您正在采用错误的方法,忽略了28天月份和31天月份之间的重叠部分。
在我进一步解释之前,让我指出,除非您被限制使用正则表达式,否则有更好的方法来解析和验证日期。最好的选择是
DateTime.TryParseExact,它专门用于此。如果您正在使用WebForms,您只需使用
Operator =“DataTypeCheck”
和
Type =“Date”
的
CompareValidator即可。
既然我说了这些神奇的话,让我们谈论正则表达式模式。
改变您看待此问题的方式。确实,并非所有月份都包含31天甚至30天,但有多少个月份包含28天?
...所有的日期! 因此,如果您想匹配四月和五月,您不需要覆盖4月1日至30日,然后再覆盖5月1日至31日。您可以编写一个模式来覆盖任一月份的前30天,然后单独匹配5月31日。结果类似于[45]/([012]?[1-9]|[123]0)|5/31
,长度只有原来的一半。
请考虑这个模式,为了清晰起见进行了扩展:
^(
( #First, we'll cover months and days for a normal year. Forget leap years
#for a second.
(
(?<month>0?[1-9]|1[012]) #First we account for up to 28 days,
/(?<day>[01]?[1-9]|10|2[0-8]) #which ALL months have.
)
|
(
(?<month>0?[13-9]|1[012]) #Every month but February has at
/(?<day>29|30) #least 30 days, so cover that.
)
|
(
(?<month>0?[13578]|1[02]) #Cover the 31st day for months
/(?<day>31) #that have one.
)
)
/(?<year>(1[89]|20)[0-9]{2}) #Any year between 1800 and 2099.
| #Normal years: Done. Now we just need to cover February 29,
#and only for leap years.
(?<month>0?2)
/(?<day>29)
/(?<year>
(1[89]|20) #Century doesn't matter, since 100 is divisible by 4.
(
[24680][048] #If the decade is even, leap years end in [048].
|
[13579][26] #If the decade is odd, leap years end in 2 or 6.
)
)
)$
我们完成了。强制使用月/日/年格式,验证现有日期,并且代码短小易读。以下是压缩版本:
^(((0?[1-9]|1[012])/([01]?[1-9]|10|2[0-8])|(0?[13-9]|1[012])/(29|30)|(0?[13578]|1[02])/31)/(1[89]|20)[0-9]{2}|0?2/29/(1[89]|20)([24680][048]|[13579][26]))$
编辑:
由于这是.NET,您可以使用回顾先行断言来缩短代码:
^(
(?!(0?[2469]|11)/31)
(?!0?2/(29|30))
(?<month>0?[1-9]|1[012])
/
(?!0?0/)
(?<day>[012]?[0-9]|3[01])
/
(?<year>(1[89]|20)[0-9]{2})
|
(?<month>0?2)/(?<day>29)
/
#Years ending in 00 are only leap years if they're divisible by 400:
(?!1[89]00)
(?<year>(1[89]|20) ( [24680][048] | [13579][26] ) )
)$
|(([2468][048]|[3579][26])00)
要在那里。 - Justin Morgan