简体   繁体   English

正则表达式日期格式小于运算符

[英]Regex date format less than operator

I am trying to use regex to verify a date format and I would like to check if the day is less than 32. Similarly, that the month is also less than 12. I have no idea how to about it. 我正在尝试使用正则表达式来验证日期格式,并且我想检查日期是否少于32。类似地,月份也少于12。我不知道如何处理。 Currently, this is what I have; 目前,这就是我所拥有的;

^[0-1]?[0-9]{1}\-[0-3]?[0-9]{1}\-[0-9]{2,4}$

This regex achieves the format (m)m-(d)d-(yy)yy 此正则表达式的格式为(m)m-(d)d-(yy)yy

TL;DR TL; DR

Don't use regular expressions for comparison operations. 不要对比较操作使用正则表达式。 Use a regex to split off values to compare, or use an actual parser. 使用正则表达式分割要比较的值,或使用实际的解析器。

Use Regular Expressions to Extract Comparables 使用正则表达式提取可比对象

Date comparisons is a really poor problem for regex to solve. 对于正则表达式来说,日期比较是一个非常糟糕的问题。 At most, you should use a regular expression to extract your days of the month for a numeric comparison. 最多,您应该使用正则表达式提取您的月份中的几天以进行数字比较。 For example: 例如:

date = '01-01-1970'
date.split('-')[1].to_i < 32
#=> true

However, the code above won't really tell you if a given date is valid . 但是,上面的代码不会真正告诉您给定日期是否有效 For example, what about February 30th or November 31st? 例如,2月30日或11月31日呢? Instead, you should attempt to parse the date to determine its validity. 相反,您应尝试解析日期以确定其有效性。

Use a Date Parser 使用日期解析器

The best way to tell if a given date is valid is to parse it with a date parser, and then report a Boolean result or handle the exception. 判断给定日期是否有效的最佳方法是使用日期解析器对其进行解析,然后报告布尔结果或处理异常。 For example, you could attempt to parse the date with Date#parse . 例如,您可以尝试使用Date#parse解析日期

Boolean Results 布尔结果

If you just want a Boolean result, you can coerce a valid/invalid parse to true or false. 如果只需要布尔结果,则可以将有效/无效的分析强制为true或false。 For example: 例如:

require 'date'

date = '01-33-1970'
!!(Date.parse date rescue nil)
#=> false

Rescuing and Reporting the Exception 抢救和报告异常

Less magically, you would need to rescue ArgumentError from Date#parse . 少魔术,您需要从Date#parse中抢救ArgumentError。 For example: 例如:

require 'date'

def valid_date? date_string
  true if Date.parse date_string
rescue ArgumentError => e
  STDERR.puts "#{e.class}: #{e}: '#{date_string}'"
  false
end

valid_date? '11-31-1970'

This will do what you expect, albeit more verbosely. 这会做您期望的,尽管更详细。 For example, the above example will print the exception to standard error, and then return false as the result. 例如,以上示例将异常打印为标准错误,然后返回false作为结果。

ArgumentError: invalid date: '11-31-1970' ArgumentError:无效日期:“ 11-31-1970”
#=> false #=>错误

^(?:[0-1][1-2]|[1-9])\-(?:3[0-1]|[0-2][1-9]|[1-9])\-[0-9]{2}(?:[0-9]{2})?$

should do what you're looking for. 应该做你想要的。 It will only allow months from 1-12 (either 1-9 or 01-12), days from 1-31 (either 1-9 or 01-31) and years of at least 2 digits with a maximum of four. 它将只允许1-12个月(1-9或01-12),1-31-31(1-9或01-31)的天以及至少2位数字(最多为四位)的年份。 Tested on regex101. 在regex101上测试。

Basic: 基本:

Here is a regex that should do what you want: 这是一个正则表达式,它可以完成您想要的操作:

^(0[1-9]|1[0-2]|[1-9])-(0[1-9]|[1-2][0-9]|3[0-1]|[1-9])-\d{2}(\d{2})?$

It matches months greater than 0 and less than 13, then - , then days greater than 0 and less than 32, then - , then years (2 digits or 4 digits). 它匹配大于0且小于13的月份,然后是- ,然后是大于0且小于32的天,然后是- ,然后是年份(2位或4位数字)。

Bonus: 奖金:

Full regex for matching dates in that format with validation: 完整正则表达式,用于通过验证匹配该格式的日期:

^((0?[13578]|10|12)-(([1-9])|(0[1-9])|([12])([0-9]?)|(3[01]?))-((19)([2-9])(\d{1})|(20)([01])(\d{1})|([8901])(\d{1}))|(0?[2469]|11)-(([1-9])|(0[1-9])|([12])([0-9]?)|(3[0]?))-((19)([2-9])(\d{1})|(20)([01])(\d{1})|([8901])(\d{1})))$

If you want to determine the string is a valid date, you'd be better off attempting to convert it. 如果要确定字符串为有效日期,最好尝试将其转换。 If it won't convert, it's not valid. 如果无法转换,则无效。

def date_valid?(date_string)
  format = '%m/%d/' + (date_string.split(-).last.size == 4 ? '%Y' : '%y')
  return true if Date.strptime(date_string, format)
  rescue ArgumentError
  return false
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM