正则表达式匹配日期中的有效日期 [英] Regular Expression to match a valid day in a date
本文介绍了正则表达式匹配日期中的有效日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助想出一个正则表达式来确保用户输入一个有效的日期字符串格式为mm/dd/yyyy
I need help coming up with a regex to make sure the user enters a valid date
The string will be in the format of mm/dd/yyyy
这是我目前想到的.
/[1-9]|0[1-9]|1[0-2]/d{1,2}/19|20dd/
我已经验证了用户不能输入大于 12 天并且年份必须以19"或20"开头的正则表达式.我遇到的问题是找出验证这一天的一些逻辑.一天不应该超过 31.
I have validated the regex where the user cannot enter a day higher than 12 and the years have to start with either "19" or "20". What I am having trouble is figuring out some logic for validating the day. The day should not go over 31.
推荐答案
Regex for 0-31:
Regex for 0-31:
(0[1-9]|[12]d|3[01])
或者,如果您不想要前导零的日子(例如 05):
Or if you don't want days with a preceding zero (e.g. 05):
([1-9]|[12]d|3[01])
这篇关于正则表达式匹配日期中的有效日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文