regex - Converting a string that is a bit Lisp-like to a Ruby array -


i have string this:

s = '+((((content:suspension) (content:sensor))~2) ((+(((content:susp) (content:sensor))~2)) (+(((content:height) (content:control) (content:sensor))~3)) (+(((content:height) (content:sensor))~2)) (+(((content:level) (content:control) (content:sensor))~3)) (+(((content:rear) (content:height) (content:sensor))~3)) (+(((content:ride) (content:height) (content:sensor))~3))))' 

i'd convert array of strings like:

["suspension sensor", "susp sensor", "height control sensor", "height sensor", "level control sensor", "rear height sensor", "ride height sensor"] 

here ugly bit of code accomplishes that:

a = s.gsub('content:', '') \   .gsub(/\+/, '') \   .gsub(/~\d/, '') \   .gsub(/\((\w+)\)/) { $1 } \   .gsub(/\(([^\(]*[^\)])\)/) { "#{$1}" } \   .gsub(/[\(\)]/,', ') \   .split(/\s?,\s?/) \   .reject {|x| x.strip == ''} 

i think there must nicer way of doing this, given if take out content: prefix , + , ~\d pieces, it's lisp expression.

s.gsub(/\(content:([^(]*)\)/, '\1').scan(/(?<=\()[^()]*(?=\))/)  # => ["suspension sensor", "susp sensor", "height control sensor", "height sensor", "level control sensor", "rear height sensor", "ride height sensor"] 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -