开发者

Reformat (crazy) strings with RegExp

My application fetches information from an API. Unfortunately their data isn't very pretty. This is what the strings can look like:

2

2:30

10.00

2 am

3:30 pm

10:00pm est

10:00pm PT/EST

10:00pm (Central)

10:00, 11:00 & 12:00

I mean, they can pretty much be anything :)

I need the format to be 12:00 (HH:MM) !

Here is what I've got so far, and it works pretty OK, but I'm no RegExp-guru :) I think there is a much faster shortcut getting there.

(Ruby code. Gsub = Replace)

def universal_hour(time)

  #replacing & (10:00 & 18:00) and dots (10.00)
  formatted_time = time.upcase.gsub("&", ",").gsub(".", ":")

  #midnight = 00:00
  formatted_time = formatted_time.gsub("midnight", "00:00")

  #removing everything else (10:00 am PST whatever), whitespaces and tailing punctuation
  formatted_time = formatted_time.gsub(/[a-z]+|[A-Z]+|\(|\)|\s|.*:$|.*,$|.*-$/, "")

  #make these formats: 10, 2, 3:30, into: HH:MM
  formatted_time = "#{formatted_time}:00" if formatted_time.match(/^\d{2}$/)
  formatted_time = "0#{formatted_time}:00" if formatted_time.match(/^\d{1}$/)
  formatted_time = "0#{formatted_time}" if formatted_time.match(/^\d{1}:\d{2}$/)

  #Some are dates (2007-01-24)
  formatted_time = "" if formatted_time.match(/^\d{4}-\d{2}-\d{2}$/)

  #Some weird things (10/9)
  formatted_time = "" if formatted_time.match(/\//)

  #Be safe: If it's still doesn开发者_StackOverflow't contain a number, remove it
  formatted_time = "" unless formatted_time.match(/\d/)

  #10-30 -> 10:30
  formatted_time = formatted_time.gsub("-", ":") if formatted_time.match(/^\d+-\d{2}$/)

  #2300 -> 23:00
  if formatted_time.match(/^\d{4}$/)
    h = formatted_time[0..1]
    m = formatted_time[2..3]
    formatted_time = "#{h}:#{m}"
  end

  return formatted_time
end


gem install chronic and your problems are solved. See http://chronic.rubyforge.org/


Instead of using regex, why not use Ruby's ability to parse and format time?

t = Time.parse("3:30 pm").strftime("%H:%M") # 15:30

The only problem with this is that it won't cover some of your corner cases (like strings with timezones, or "midnight"). For that, you can use a time parsing gem, like Chronic.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜