特定日期格式的正则表达式 [英] Regular Expression of a specific Date format

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

问题描述

我需要形成日期的正则表达式以匹配YYYYMMDD格式的帮助.详细信息如下:

I need help in forming a regular expression of a date to match YYYYMMDD format. Below are the details:

输入验证:输入的值是8的长度和所有数字.这 第一个字符是1或2.第六个字符介于01和12之间 最后两个字符在01和31之间

Input Validation: The value entered is length of 8 and all numeric. The first character is a 1 or 2. The 5th & 6th character are between 01 and 12, the last two characters are between 01 and 31

我尝试在表达式下进行尝试,但无法理解我们如何提供值范围,例如1-12个月和1-31天.

I tried it below expression but cannot understand how we can provide the value ranges like 1-12 for months and 1-31 for days.

SELECT 'P' from dual where REGEXP_LIKE ('122412','^[1][2][0-9]{2}[1|12]$');

提前谢谢!

推荐答案

示例:

^[12][0-9]{3}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$

Debuggex演示

警告

使用regex并不是一个好主意,因为它会传递2014022920140431等无效值.请检查@Ben的答案以获取正确的方法.

It's not good idea to use regex for that, since it will pass invalid values like 20140229, 20140431, etc. Check @Ben's answer for proper way.

如果您想验证日期时间并且无法像@Ben的回答那样在Oracle中创建函数(无访问权限)

然后您可以使用以下查询:

Then you can use the following query:

SELECT 'P'
FROM DUAL
WHERE REGEXP_LIKE ('20140229','^[12][0-9]{3}(0[1-9]|1[0-2])(0[1-9]|[12][0-9]|3[01])$')
      AND '20140229' <= TO_CHAR(LAST_DAY(TO_DATE(SUBSTR('20140229', 1, 6) || '01', 'YYYYMMDD')), 'YYYYMMDD');

您确实将20140229替换为列名.

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

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