How i can to parse string

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

On Apr 26, 2006, at 6:37 AM, 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

What have you tried? Where are you stuck?

Show us your attempts and we will be happy to help you fix them…

James Edward G. II

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

On 4/26/06, James Edward G. II [email protected] wrote:

That is my version for problem solving.
puts parameter.inspect
[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”}

Cameron

On Apr 26, 2006, at 7:12 AM, Jonh wrote:

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”}

James Edward G. II

Hi –

On Wed, 26 Apr 2006, Cameron McBride wrote:

end
[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 :slight_smile:

I think this does:

Hash[*str.scan(/([^,]+)=([^,]+)/).flatten]

David


David A. Black ([email protected])
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

“Ruby for Rails” PDF now on sale! Ruby for Rails
Paper version coming in early May!

On Apr 26, 2006, at 8:02 AM, [email protected] wrote:

store[name]=Ilyas store,store[phone]= 4999-233-2923, store[fax]=
if value =~ /(.)=(.)/
=lkjklsdaj,save=save,cancel=cancel"
“phone”=>“4999-233-2923”}

But… but… neither of you guys’s version gives the same result as
Jonh’s :slight_smile:

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. :wink:

James Edward G. II

On Apr 26, 2006, at 7:56 AM, Cameron McBride wrote:

end
=> {“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. :wink: I imagine they aren’t needed though and your way is much
better.

James Edward G. II