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.
51abb2c428788c12d4f3ea19aaa2b350?d=identicon&s=25 Jonh (Guest)
on 2006-04-26 13: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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-26 13: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 Gray II
51abb2c428788c12d4f3ea19aaa2b350?d=identicon&s=25 Jonh (Guest)
on 2006-04-26 14: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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-26 14: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 Gray II
A4a4095ff08bd0fced3c3fddbeac743a?d=identicon&s=25 Cameron McBride (Guest)
on 2006-04-26 14:57
(Received via mailing list)
On 4/26/06, James Edward Gray II <james@grayproductions.net> 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
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-04-26 15: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 (dblack@wobblini.net)
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!
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-26 15: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 Gray II
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-04-26 15:13
(Received via mailing list)
On Apr 26, 2006, at 8:02 AM, dblack@wobblini.net 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 Gray II
This topic is locked and can not be replied to.