I’d like to convert a datetime format string like "%m/%d/%y %H:%M" into a regular expression. I won’t know what is in the format string, and I don’t have to handle every case, so I’ve been trying to just gsub into the string.
DATEFORMAT = "%m/%d/%y %H:%M"
def date_format_to_regex
format = DATEFORMAT
format = format.gsub(/\//,'\/')
format = format.gsub(/%[ymdCHI]/,'[[:digit:]]{2}') #two digits
format = format.gsub(/%[YMS]/,'[[:digit:]]{4}') #four digits
@date_regex = Regexp.new(format)
end
But the output of that screws up a lot of the slashes
"%m/%d/%y %H:%M == (?-mix:[[:digit:]]{2}\\/[[:digit:]]{2}\\/[[:digit:]]{2} [[:digit:]]{2}:[[:digit:]]{4})"
1 Answer