有谁知道英国日期格式的reg表达式 [英] Does anyone know of a reg expression for uk date format

查看:38
本文介绍了有谁知道英国日期格式的reg表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人知道英国日期格式的正则表达式,例如日/月/年.

Hi does any one know a reg ex for a uk date format e.g. dd/mm/yyyy.

dd 或 mm 可以是 1 个字符,例如1/1/2010 但年份必须始终为 4 个字符.

The dd or mm can be 1 character e.g. 1/1/2010 but the year must always be 4 characters.

提前致谢

推荐答案

^\d{1,2}/\d{1,2}/\d{4}$

将匹配 1/1/2000、07/05/1999,以及 99/77/8765.

will match 1/1/2000, 07/05/1999, but also 99/77/8765.

因此,如果您想进行一些基本的合理性检查,您需要

So if you want to do some rudimentary plausibility checking, you need

^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/\d{4}$

这仍然会匹配 31/02/9999,所以如果你想抓住那些,它会变得更加毛茸茸:

This will still match 31/02/9999, so if you want to catch those, it's getting hairier:

^(?:(?:[12][0-9]|0?[1-9])/0?2|(?:30|[12][0-9]|0?[1-9])/(?:0?[469]|11)|(?:3[01]|[12][0-9]|0?[1-9])/(?:0?[13578]|1[02]))/\d{4}$

但这仍然不会赶上闰年.因此,修改 来自 regexlib.com 的正则表达式的野兽:

But this still won't catch leap years. So, modifying a beast of a regex from regexlib.com:

^(?:(?:(?:(?:31\/(?:0?[13578]|1[02]))|(?:(?:29|30)\/(?:0?[13-9]|1[0-2])))\/(?:1[6-9]|[2-9]\d)\d{2})|(?:29\/0?2\/(?:(?:(1[6-9]|[2-9]\d)(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:0?[1-9]|1\d|2[0-8])\/(?:(?:0?[1-9])|(?:1[0-2]))\/(?:(?:1[6-9]|[2-9]\d)\d{2}))$

将匹配

1/1/2001
31/5/2010
29/02/2000
29/2/2400
23/5/1671
01/1/9000

然后失败

31/2/2000
31/6/1800
12/12/90
29/2/2100
33/3/3333

总而言之,正则表达式或许可以匹配日期;验证它们不是他们的强项,但如果您可以使用它们,那当然是可能的.但看起来很可怕:)

All in all, regular expressions may be able to match dates; validating them is not their forte, but if they are all you can use, it's certainly possible. But looks horrifying :)

这篇关于有谁知道英国日期格式的reg表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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