From: James Edward Gray II Date: 2006-04-26T22:10:50+09:00 Subject: Re: How i can to parse string On Apr 26, 2006, at 8:02 AM, dblack@wobblini.net wrote: > Hi -- > > On Wed, 26 Apr 2006, 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"} > > But... but... neither of you guys's version gives the same result as > Jonh's :-) > > I think this does: > > Hash[*str.scan(/([^,]+)=([^,]+)/).flatten] We decided he didn't really want those spaces. We're always forcing our opinions on others. Luckily, David is nice than we are. ;) James Edward Gray II