![]() |
1
1
一种可能是通过
即
其中一个限制是,它不会处理日期的某些部分的前导0,例如,如果您想接受2010-6-1,但是
另外,我不确定这是不是你的意思,但你的第二个例子
|
![]() |
2
0
一种方法是定义一个首先进行验证的新类。 require 'date' class Mydate def self.strptime(string) raise ArgumentError 'Fail' if string !~ /^\d\d\d\d-\d\d-\d\d$/ Date.strptime(string) end end d = Mydate.strptime '2001-01-01' puts d d2 = Mydate.strptime '2001-01-02xxxx' puts d2 另一种方法是打开日期类,为strptime方法加别名,编写一个新的方法来执行所需的验证,然后调用别名方法。 require 'date' class Date class << self alias :orig_strptime :strptime end def self.strptime(string) puts "in new strptime" raise ArgumentError 'Fail' if string !~ /^\d\d\d\d-\d\d-\d\d$/ Date.orig_strptime(string) end end d = Date.strptime '2001-01-01' puts d d2 = Date.strptime '2001-01-02xxxx' puts d2 |
![]() |
batman · 如何用特定模式grep特定行及其子网行? 2 年前 |
![]() |
Jensen Holm · 在非常大的字符串中查找链接时遇到问题 2 年前 |
![]() |
MBF · PHP导入/解析XML文件内容保存到数据库 2 年前 |
![]() |
user10717742 · 用java编写的自定义文件解析器需要改进 2 年前 |
![]() |
Muhsin Muhammed · 向文件中的行添加引号和逗号 2 年前 |
![]() |
Felkru · 添加字符串会在Javascript中返回空字符串 2 年前 |
![]() |
Mustard Tiger · 熊猫解析文本列 2 年前 |