simple regexp question

A

Ak 756

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?
 
C

Cameron Matheson

Hello,

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
 
H

Heesob Park

Hi,

Ak said:
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 Heesob
 
A

Ak 756

Heesob said:
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 Heesob

Hi Heesob

Thanks very much.
Great csv module! Everything becoms so easy with it. -:)
 
A

Ari Brown

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top