正则表达式:使用多个分隔符验证欧洲日期格式 [英] regex: validate european date format with multiple separators

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

问题描述

我想验证欧洲日期格式,例如10.02.2012"或10-02-2012".因此我创建了以下正则表达式:

I want to validate european date formats like "10.02.2012" or "10-02-2012". Therefore I created the following regex:

/\d[0-9]{2}(.|-)\d[0-9]{2}(.|-)\d[0-9]{4}/

不幸的是,即使日期格式正确,我也总是收到无效的消息.

Unfortunately I always get the message not valid, even when the date has the right format.

当我用."替换(.|-)"时仅用于验证用点分隔的日期.所以我想问题一定是(.|-)"有什么想法吗?

When I replace the "(.|-)" with "." for validating only dates separated with a dot it works. So i guess the problem must be the "(.|-)" Any ideas?

推荐答案

稍作修改即可正常工作:

It works correctly after minor modifications :

\d{2}(\.|-)\d{2}(\.|-)\d{4}

  1. ."需要转义.

  1. the "." needs to be escaped.

\d 与 [0-9] 相同,无需重复.

\d is same as [0-9] , no need to repeat it.

http://regexr.com?326f2

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

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