Forum: Ruby How i can to parse string

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Jonh (Guest)
on 2006-04-26 15:36
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
James G. (Guest)
on 2006-04-26 15:46
(Received via mailing list)
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 (Guest)
on 2006-04-26 16:12
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
James G. (Guest)
on 2006-04-26 16:39
(Received via mailing list)
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
Cameron McBride (Guest)
on 2006-04-26 16:57
(Received via mailing list)
On 4/26/06, James Edward G. II <removed_email_address@domain.invalid> 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
unknown (Guest)
on 2006-04-26 17:04
(Received via mailing list)
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 :-)

I think this does:

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


David

--
David A. Black (removed_email_address@domain.invalid)
Ruby Power and Light, LLC (http://www.rubypowerandlight.com)

"Ruby for Rails" PDF now on sale!  http://www.manning.com/black
Paper version coming in early May!
James G. (Guest)
on 2006-04-26 17:07
(Received via mailing list)
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.  ;)  I imagine they aren't needed though and your way is much
better.

James Edward G. II
James G. (Guest)
on 2006-04-26 17:13
(Received via mailing list)
On Apr 26, 2006, at 8:02 AM, removed_email_address@domain.invalid 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 :-)
>
> 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 G. II
This topic is locked and can not be replied to.