parsing strings

I'm trying to parse ical strings (not that ical strings are all that special) but I'm trying to break them down for date calculations...

so if I have a value like this...

@events[0].recurrence_rules => ["FREQ=YEARLY;INTERVAL=1;UNTIL=99991231"]

this seems so damn clumsy...

freq = "yearly" if @events[0].recurrence_rules.to_s =~ /FREQ=YEARLY/ freq = "monthly" if @events[0].recurrence_rules.to_s =~ /FREQ=MONTHLY/

and then the interval, I would really just like the numerical value...

how can I cleanly get these values because reg expressions seem so clumsy

Craig

Hi Craig,

The following regex will produce a hash for you with key/value pairs as defined in that rules string you provided:-

recurrence_rules = "FREQ=YEARLY;INTERVAL=1;UNTIL=99991231".downcase Hash[*recurrence_rules.scan(/([^=;][a-z1-9]*)/).zip.flatten]

Beneath is my irb output so you can see how i thought you could use it:-

recurrence_rules = "FREQ=YEARLY;INTERVAL=1;UNTIL=99991231".downcase

=> "freq=yearly;interval=1;until=99991231"

ical = Hash[*recurrence_rules.scan(/([^=;][a-z1-9]*)/).zip.flatten]

=> {"freq"=>"yearly", "until"=>"99991231", "interval"=>"1"}

ical.each_key {|key| puts "#{key}=>#{ical[key]}"}

freq=>yearly until=>99991231 interval=>1 => {"freq"=>"yearly", "until"=>"99991231", "interval"=>"1"}

Hope that is ok?

Regards, Jabbslad

Hi Craig,

The following regex will produce a hash for you with key/value pairs as defined in that rules string you provided:-

recurrence_rules = "FREQ=YEARLY;INTERVAL=1;UNTIL=99991231".downcase Hash[*recurrence_rules.scan(/([^=;][a-z1-9]*)/).zip.flatten]

Beneath is my irb output so you can see how i thought you could use it:-

>> recurrence_rules = "FREQ=YEARLY;INTERVAL=1;UNTIL=99991231".downcase => "freq=yearly;interval=1;until=99991231"

>> ical = Hash[*recurrence_rules.scan(/([^=;][a-z1-9]*)/).zip.flatten] => {"freq"=>"yearly", "until"=>"99991231", "interval"=>"1"}

>> ical.each_key {|key| puts "#{key}=>#{ical[key]}"} freq=>yearly until=>99991231 interval=>1 => {"freq"=>"yearly", "until"=>"99991231", "interval"=>"1"}

Hope that is ok?