正则表达式匹配M / YYYY,MM / YYYY,M / YY或MM / YY格式 [英] RegEx to match M/YYYY, MM/YYYY , M/YY or MM/YY format

查看:622
本文介绍了正则表达式匹配M / YYYY,MM / YYYY,M / YY或MM / YY格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要帮助寻找或有一个正则表达式匹配一个MM / YY或MM / YYYY格式。我RegExFu弱,我甚至不知道从哪里开始写这个。

Need help finding or having a RegEx match a MM/YY or MM/YYYY format. My RegExFu is weak and I'm not even sure where to begin writing this.

月应该是1-12,年,以及2009年的任何超越应该是有效的。对不起,之前没有提到更多细节。这被用作到期日期。

Months should be 1-12, years, well anything beyond 2009 should be valid. Sorry for not mentioning more details before. This is used as an expiration date.

我会添加对任何人超出去和验证MM / YY或MM / YYYY格式,> =今天的日期赏金。无感让过去的第一个验证层过期的东西。

I'll add a bounty for whomever goes above and beyond and validates MM/YY or MM/YYYY format that is >= today's date. No sense letting expired stuff past the first validation layer.

我觉得不好,因为我改变了我的要求,不得不更具体什么我需要中期的问题,所以我会奖赏金所有那些谁回答,一旦没有赏金窗口过期。

I feel bad since I changed my requirements and had to be more specific in what I needed mid-question so I'll award bounties to all those who answered once the no-bounty window expires.

推荐答案

什么

^(1[0-2]|0[1-9]|\d)\/(20\d{2}|19\d{2}|0(?!0)\d|[1-9]\d)$

匹配月

// 10 to 12 | 01 to 09 | 1 to 9
(1[0-2]|0[1-9]|\d)

和年

// 2000 to 2099 | 1900 to 1999
// 01 to 09 | 10 to 99
(20\d{2}|19\d{2}|0(?!0)\d|[1-9]\d)






要匹配任何> = 2010

/^(1[0-2]|0[1-9]|\d)\/([2-9]\d[1-9]\d|[1-9]\d)$/;



结果:

Result:

12/2009 : false
1/2010  : true
12/2011 : true
12/9011 : true
12/07   : false
12/17   : true

这篇关于正则表达式匹配M / YYYY,MM / YYYY,M / YY或MM / YY格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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