From: James Edward Gray II Date: 2006-04-26T22:04:24+09:00 Subject: Re: How i can to parse string On Apr 26, 2006, at 7:56 AM, Cameron McBride wrote: > On 4/26/06, James Edward Gray II wrote: >> On Apr 26, 2006, at 7:12 AM, Jonh wrote: >> >>> Jonh wrote: >>>> I have this string: >>>> >>>> store[name]=Ilyas store,store[phone]= 4999-233-2923, store[fax]= >>>> 80233923293,store[description]=lkjklsdaj,save=save,cancel=cancel >>>> >>>> need with regexp, put it's in Hash object >>> >>> That is my version for problem solving. >>> I want to simplify it >>> >>> if params[:param] >>> paramater = Hash.new >>> for value in params[:param].split(/,/) >>> if value =~ /(.*)=(.*)/ >>> parameter[$1] = $2 >>> end >>> end >>> end >>> puts parameter.inspect >> >> Does this help? >> >>>> str = "store[name]=Ilyas store,store[phone]= 4999-233-2923, store >> [fax]= 80233923293,store[description] >> =lkjklsdaj,save=save,cancel=cancel" >> => "store[name]=Ilyas store,store[phone]= 4999-233-2923, store[fax]= >> 80233923293,store[description]=lkjklsdaj,save=save,cancel=cancel" >>>> Hash[*str.scan(/([^\s,]+)\s*=\s*([^,]+)/).flatten] >> => {"cancel"=>"cancel", "store[fax]"=>"80233923293", "store >> [name]"=>"Ilyas store", "save"=>"save", "store >> [description]"=>"lkjklsdaj", "store[phone]"=>"4999-233-2923"} > > or the slight modification to James' answer: > irb> store = Hash[*str.scan(/\S\[([^\s,]+)\]\s*=\s*([^,]+)/).flatten] > => {"name"=>"Ilyas store", "description"=>"lkjklsdaj", > "phone"=>"4999-233-2923"} I was going to do that too, until I realized that it lost fields. ;) I imagine they aren't needed though and your way is much better. James Edward Gray II