# Simple regexp question

**URL:** <https://www.ruby-forum.com/t/simple-regexp-question/115746>\
**Category:** Ruby\
**Created:** [September 28, 2007, 5:30am UTC](https://www.ruby-forum.com/t/simple-regexp-question/115746 "2007-09-28T05:30:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ak756](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b5a626/32.png) [@ak756](https://www.ruby-forum.com/u/ak756)\
**Post date:** [September 28, 2007, 5:30am UTC](https://www.ruby-forum.com/t/simple-regexp-question/115746/1 "2007-09-28T05:30:57Z")

</div>

Hi

I have a .txt file which contains one line data as

“George”,33,“engineer, r&d department, this company”,“234-7554-344”

I use name,age,memo,tel=File.open(filename).split(’,’) to collect values  
but get wrong value for memo and tel. I want string “engineer, r&d  
department, this company” not be splited by comma, how can I do that?

---

<div class="post-metadata">

**Author:** ![ak756](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b5a626/32.png) [@ak756](https://www.ruby-forum.com/u/ak756)\
**Post date:** [September 28, 2007, 6:39am UTC](https://www.ruby-forum.com/t/simple-regexp-question/115746/2 "2007-09-28T06:39:30Z")

</div>

Hello,

On 9/27/07, Ak 756 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have a .txt file which contains one line data as
> 
> “George”,33,“engineer, r&d department, this company”,“234-7554-344”
> 
> I use name,age,memo,tel=File.open(filename).split(‘,’) to collect values  
> but get wrong value for memo and tel. I want string “engineer, r&d  
> department, this company” not be splited by comma, how can I do that?

I’m sure there’s a more elegant solution, but this worked for me  
(assuming the string is stored in the variable s):

name, age, memo, tel = $1, $2, $3, $4 if s =~  
/^“(.+?)”,(\d+),“(.+?)”,“([-\d+]+)”/

Cam

---

<div class="post-metadata">

**Author:** ![ak756](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b5a626/32.png) [@ak756](https://www.ruby-forum.com/u/ak756)\
**Post date:** [September 28, 2007, 7:46am UTC](https://www.ruby-forum.com/t/simple-regexp-question/115746/3 "2007-09-28T07:46:42Z")

</div>

Heesob P. wrote:

> Why not use csv module?
> 
> require ‘csv’  
> data = ‘“George”,33,“engineer, r&d department, this  
> company”,“234-7554-344”’  
> name,age,memo,tel=CSV.parse\_line(data)
> 
> Regards,
> 
> Park H.

Hi Heesob

Thanks very much.  
Great csv module! Everything becoms so easy with it. -🙂

---

<div class="post-metadata">

**Author:** ![ak756](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b5a626/32.png) [@ak756](https://www.ruby-forum.com/u/ak756)\
**Post date:** [September 28, 2007, 7:03am UTC](https://www.ruby-forum.com/t/simple-regexp-question/115746/4 "2007-09-28T07:03:58Z")

</div>

Hi,

Ak 756 wrote:

> Hi
> 
> I have a .txt file which contains one line data as
> 
> “George”,33,“engineer, r&d department, this company”,“234-7554-344”
> 
> I use name,age,memo,tel=File.open(filename).split(’,’) to collect values  
> but get wrong value for memo and tel. I want string “engineer, r&d  
> department, this company” not be splited by comma, how can I do that?

Why not use csv module?

require ‘csv’  
data = ‘“George”,33,“engineer, r&d department, this  
company”,“234-7554-344”’  
name,age,memo,tel=CSV.parse\_line(data)

Regards,

Park H.

---

<div class="post-metadata">

**Author:** ![ak756](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b5a626/32.png) [@ak756](https://www.ruby-forum.com/u/ak756)\
**Post date:** [September 28, 2007, 1:23pm UTC](https://www.ruby-forum.com/t/simple-regexp-question/115746/5 "2007-09-28T13:23:58Z")

</div>

On Sep 27, 2007, at 11:31 PM, Ak 756 wrote:

> Hi
> 
> I have a .txt file which contains one line data as
> 
> “George”,33,“engineer, r&d department, this company”,“234-7554-344”
> 
> I use name,age,memo,tel=File.open(filename).split(’,’) to collect  
> values  
> but get wrong value for memo and tel. I want string “engineer, r&d  
> department, this company” not be splited by comma, how can I do that?

fastercsv might help. It’s great, AND a lot faster for parsing comma  
separated values.

~ Ari  
English is like a pseudo-random number generator - there are a  
bajillion rules to it, but nobody cares.
