Regular Expression to match valid dates

后端 未结 15 1847
庸人自扰
庸人自扰 2020-11-22 04:48

I\'m trying to write a regular expression that validates a date. The regex needs to match the following

  • M/D/YYYY
  • MM/DD/YYYY
  • Single digit mon
15条回答
  •  -上瘾入骨i
    2020-11-22 05:14

    I know this does not answer your question, but why don't you use a date handling routine to check if it's a valid date? Even if you modify the regexp with a negative lookahead assertion like (?!31/0?2) (ie, do not match 31/2 or 31/02) you'll still have the problem of accepting 29 02 on non leap years and about a single separator date format.

    The problem is not easy if you want to really validate a date, check this forum thread.

    For an example or a better way, in C#, check this link

    If you are using another platform/language, let us know

提交回复
热议问题