Better than a crazy huge Regex (assuming this is for validation and not scanning):
require 'date'
def valid_date?( str, format="%m/%d/%Y" )
Date.strptime(str,format) rescue false
end
And as an editorial aside: Eww! Why would you use such a horribly broken date format? Go for ISO8601, YYYY-MM-DD
, which is a valid international standard, has a consistent ordering of parts, and sorts lexicographically as well.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…