如何使用正则表达式验证日期? [英] How Do I Validate Date With Regex?

查看:111
本文介绍了如何使用正则表达式验证日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以相同的格式验证格式MM / DD / YYYY的日期。

请帮助

是否可以提取日期范围正则表达式?

解决方案

不要使用正则表达式:你可以这样做,但是当你开始考虑28天,30天和31天时它非常复杂几个月,然后有闰年...

尝试:

 DateTime dt; 
if(DateTime.TryParseExact(04/17/2015,MM / dd / yyyy,CultureInfo(en-US),DateTimeStyles.None,out dt))
{
...


尝试(((0 | 1)[1-9] | 2 [1-9] | 3 [0-1])\ /(0 [1-9] | 1 [0-2])\ /((19 | 20)\d\d))

作为表达。


I am trying to validate date in format MM/DD/YYYY exactly in the same format.
Please help
And is it possible to extract range of date with regex?

解决方案

Don't use a regex: you can do it, but it's horribly complex when you start thinking about 28 day months, 30 day, and 31 day months, and then there are leap years...
Try:

DateTime dt;
if (DateTime.TryParseExact("04/17/2015", "MM/dd/yyyy", CultureInfo("en-US"), DateTimeStyles.None, out dt))
   {
   ...


Try "(((0|1)[1-9]|2[1-9]|3[0-1])\/(0[1-9]|1[0-2])\/((19|20)\d\d))


" as your expression.


这篇关于如何使用正则表达式验证日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆