# How i can to parse string

**URL:** <https://rubytalk.org/t/how-i-can-to-parse-string/26947>\
**Category:** ruby-talk\
**Created:** [26 April 2006 11:37 UTC](https://rubytalk.org/t/how-i-can-to-parse-string/26947 "2006-04-26T11:37:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![John](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@John](https://rubytalk.org/u/John)\
**Post date:** [26 April 2006 11:37 UTC](https://rubytalk.org/t/how-i-can-to-parse-string/26947/1 "2006-04-26T11:37:11Z")

</div>

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [26 April 2006 11:43 UTC](https://rubytalk.org/t/how-i-can-to-parse-string/26947/2 "2006-04-26T11:43:08Z")

</div>

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

> **···**
>
> 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

---

<div class="post-metadata">

**Author:** ![John](https://avatars.discourse-cdn.com/v4/letter/j/a3d4f5/32.png) [@John](https://rubytalk.org/u/John)\
**Post date:** [26 April 2006 12:12 UTC](https://rubytalk.org/t/how-i-can-to-parse-string/26947/3 "2006-04-26T12:12:49Z")

</div>

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]  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paramater = Hash.new  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for value in params[:param].split(/,/)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if value =~ /(.\*)=(.\*)/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameter[$1] = $2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
end  
puts parameter.inspect

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [26 April 2006 12:37 UTC](https://rubytalk.org/t/how-i-can-to-parse-string/26947/4 "2006-04-26T12:37:54Z")

</div>

> 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]  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paramater = Hash.new  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for value in params[:param].split(/,/)  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if value =~ /(.\*)=(.\*)/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameter[$1] = $2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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

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

---

<div class="post-metadata">

**Author:** ![Cameron\_McBride](https://avatars.discourse-cdn.com/v4/letter/c/a87d85/32.png) [@Cameron\_McBride](https://rubytalk.org/u/Cameron_McBride)\
**Post date:** [26 April 2006 12:56 UTC](https://rubytalk.org/t/how-i-can-to-parse-string/26947/5 "2006-04-26T12:56:45Z")

</div>

> \> 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:

> store = Hash[\*str.scan(/\S\[([^\s,]+)\]\s\*=\s\*([^,]+)/).flatten]

=\> {"name"=\>"Ilyas store", "description"=\>"lkjklsdaj", "phone"=\>"4999-233-2923"}

Cameron

> **···**
>
> On 4/26/06, James Edward Gray II \<james@grayproductions.net\> wrote:
> 
> > On Apr 26, 2006, at 7:12 AM, Jonh wrote:

---

<div class="post-metadata">

**Author:** ![David\_A\_Black3](https://avatars.discourse-cdn.com/v4/letter/d/6a8cbe/32.png) [@David\_A\_Black3](https://rubytalk.org/u/David_A_Black3)\
**Post date:** [26 April 2006 13:02 UTC](https://rubytalk.org/t/how-i-can-to-parse-string/26947/6 "2006-04-26T13:02:56Z")

</div>

Hi --

> **···**
>
> On Wed, 26 Apr 2006, Cameron McBride wrote:
> 
> > On 4/26/06, James Edward Gray II \<james@grayproductions.net\> 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]  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paramater = Hash.new  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for value in params[:param].split(/,/)  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if value =~ /(.\*)=(.\*)/  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameter[$1] = $2  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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:  
> > \> 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](http://www.rubypowerandlight.com))
> 
> "Ruby for Rails" PDF now on sale! [Ruby for Rails](http://www.manning.com/black)  
> Paper version coming in early May!

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [26 April 2006 13:04 UTC](https://rubytalk.org/t/how-i-can-to-parse-string/26947/7 "2006-04-26T13:04:24Z")

</div>

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

> **···**
>
> On Apr 26, 2006, at 7:56 AM, Cameron McBride wrote:
> 
> > On 4/26/06, James Edward Gray II \<james@grayproductions.net\> 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]  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paramater = Hash.new  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for value in params[:param].split(/,/)  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if value =~ /(.\*)=(.\*)/  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameter[$1] = $2  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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:  
> > \> store = Hash[\*str.scan(/\S\[([^\s,]+)\]\s\*=\s\*([^,]+)/).flatten]  
> > =\> {"name"=\>"Ilyas store", "description"=\>"lkjklsdaj", "phone"=\>"4999-233-2923"}

---

<div class="post-metadata">

**Author:** ![James\_Edward\_Gray\_II](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@James\_Edward\_Gray\_II](https://rubytalk.org/u/James_Edward_Gray_II)\
**Post date:** [26 April 2006 13:10 UTC](https://rubytalk.org/t/how-i-can-to-parse-string/26947/8 "2006-04-26T13:10:50Z")

</div>

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

> **···**
>
> 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 \<james@grayproductions.net\> 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]  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;paramater = Hash.new  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for value in params[:param].split(/,/)  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if value =~ /(.\*)=(.\*)/  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parameter[$1] = $2  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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:  
> > > \> 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]
