# String parse

**URL:** <https://www.ruby-forum.com/t/string-parse/112079>\
**Category:** Ruby\
**Created:** [August 15, 2007, 1:01am UTC](https://www.ruby-forum.com/t/string-parse/112079 "2007-08-15T01:01:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anitawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@anitawa](https://www.ruby-forum.com/u/anitawa)\
**Post date:** [August 15, 2007, 1:01am UTC](https://www.ruby-forum.com/t/string-parse/112079/1 "2007-08-15T01:01:19Z")

</div>

Hello,

I want to be able to parse a string and put them into variables.

for example, I have this string:

“Menu: steak\_and\_egg | date: 0814 | who: Anita”

I want to parse this string to assign variables like so:

menu = “steak\_and\_egg”  
date = “0814”  
who = “Anita”

What would be the fastest way of doing this.

Thanks

---

<div class="post-metadata">

**Author:** ![anitawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@anitawa](https://www.ruby-forum.com/u/anitawa)\
**Post date:** [August 15, 2007, 1:17am UTC](https://www.ruby-forum.com/t/string-parse/112079/2 "2007-08-15T01:17:20Z")

</div>

On Aug 14, 2007, at 6:00 PM, anitawa wrote:

> menu = “steak\_and\_egg”  
> date = “0814”  
> who = “Anita”
> 
> What would be the fastest way of doing this.

This might work if your data isn’t too complicated:

> > str = “Menu: steak\_and\_egg | date: 0814 | who: Anita”  
> > =\> “Menu: steak\_and\_egg | date: 0814 | who: Anita”
> 
> > Hash[_str.scan(/(\w+):\s_(\w+)/).flatten]  
> > =\> {“date”=\>“0814”, “who”=\>“Anita”, “Menu”=\>“steak\_and\_egg”}

James Edward G. II

---

<div class="post-metadata">

**Author:** ![anitawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@anitawa](https://www.ruby-forum.com/u/anitawa)\
**Post date:** [August 15, 2007, 5:12am UTC](https://www.ruby-forum.com/t/string-parse/112079/3 "2007-08-15T05:12:32Z")

</div>

James Edward G. II wrote:

> > I want to parse this string to assign variables like so:  
> > =\> “Menu: steak\_and\_egg | date: 0814 | who: Anita”
> > 
> > > Hash[_str.scan(/(\w+):\s_(\w+)/).flatten]  
> > > =\> {“date”=\>“0814”, “who”=\>“Anita”, “Menu”=\>“steak\_and\_egg”}

Might be more general than is needed. If you know in advance that the  
“fields” are menu, date, and who, then this will do:

str = “Menu: steak\_and\_egg | date: 0814 | who: Anita”  
pat = /Menu: (\S+) | date: (\S+) | who: (\S+)/

menu, date, who = str.scan(pat)[0]

---

<div class="post-metadata">

**Author:** ![anitawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@anitawa](https://www.ruby-forum.com/u/anitawa)\
**Post date:** [August 15, 2007, 11:40pm UTC](https://www.ruby-forum.com/t/string-parse/112079/4 "2007-08-15T23:40:57Z")

</div>

On Aug 14, 8:12 pm, Joel VanderWerf [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > for example, I have this string:
> 
> str = “Menu: steak\_and\_egg | date: 0814 | who: Anita”  
> pat = /Menu: (\S+) | date: (\S+) | who: (\S+)/
> 
> menu, date, who = str.scan(pat)[0]
> 
> –  
> vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Thanks, just what i was looking for

---

<div class="post-metadata">

**Author:** ![anitawa](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/779978/32.png) [@anitawa](https://www.ruby-forum.com/u/anitawa)\
**Post date:** [August 15, 2007, 8:28pm UTC](https://www.ruby-forum.com/t/string-parse/112079/5 "2007-08-15T20:28:24Z")

</div>

Le mercredi 15 août 2007 01:00, anitawa a écrit :

> menu = “steak\_and\_egg”  
> date = “0814”  
> who = “Anita”
> 
> What would be the fastest way of doing this.
> 
> Thanks

This is a CSV format, You can use the csv lib from the stdlib :

# parse with : and | as delimiters

str = “Menu: steak\_and\_egg | date: 0814 | who: Anita”  
parsed = CSV.parse(str, “:”, “|”)  
=\> [[“Menu”, " steak\_and\_egg “], [” date", " 0814 “], [” who", "  
Anita"]]

# then, put the results in a hash

res = {}  
parsed.each do |k, v|  
res[k.strip] = v.strip  
end  
=\> {“date”=\>“0814”, “who”=\>“Anita”, “Menu”=\>“steak\_and\_egg”}

You can also use Enumerable#inject for putting in the Hash (or Hash::[]  
with  
some adaptations)
