Grabbing quoted substrings

B

Bil Kleb

OK, I give up, what's the elegant method to
grab the quoted substrings in the following,

a = 'variables="x", "y", "z", "rho", "u", "v", "w", "p/pinf", "s", "mach"'

I want an array like,

["x", "y", "z", ..., "mach"]

The methods I've come up with so far are
embarrassingly hideous.

Thanks,
 
M

mitchell

Hi,
OK, I give up, what's the elegant method to
grab the quoted substrings in the following,

a = 'variables="x", "y", "z", "rho", "u", "v", "w", "p/pinf", "s", "mach"'

I want an array like,

["x", "y", "z", ..., "mach"]

a.scan(/"([^"]+)"/).flatten
 
T

Tom Werner

Bil said:
OK, I give up, what's the elegant method to
grab the quoted substrings in the following,

a = 'variables="x", "y", "z", "rho", "u", "v", "w", "p/pinf", "s",
"mach"'

I want an array like,

["x", "y", "z", ..., "mach"]

The methods I've come up with so far are
embarrassingly hideous.

This should do the trick:

a.scan(/"(.*?)"/).flatten

Tom Werner
 
A

ara.t.howard

OK, I give up, what's the elegant method to
grab the quoted substrings in the following,

a = 'variables="x", "y", "z", "rho", "u", "v", "w", "p/pinf", "s", "mach"'

I want an array like,

["x", "y", "z", ..., "mach"]

The methods I've come up with so far are
embarrassingly hideous.

easy:

harp:~ > cat a.rb
a = 'variables="x", "y", "z", "rho", "u", "v", "w", "p/pinf", "s", "mach"'

variables = eval a

p variables


harp:~ > ruby a.rb
["x", "y", "z", "rho", "u", "v", "w", "p/pinf", "s", "mach"]


hard:

harp:~ > cat a.rb
a = 'variables="x", "y", "z", "rho", "u", "v", "w", "p/pinf", "s", "mach"'

re = %r/
"([^"\\]*(?:\\.[^"\\]*)*)" # matches balanced double quotes
|
'([^'\\]*(?:\\.[^'\\]*)*)' # matches balanced single quotes
/ox

variables = a.scan(re).map{|a,b| a || b}

p variables


harp:~ > ruby a.rb
["x", "y", "z", "rho", "u", "v", "w", "p/pinf", "s", "mach"]


info:

http://rubyforge.org/pipermail/bdrg-members/2006-June/000050.html

cheers.

-a
 
T

Tom Werner

OK, I give up, what's the elegant method to
grab the quoted substrings in the following,

a = 'variables="x", "y", "z", "rho", "u", "v", "w", "p/pinf", "s",
"mach"'

I want an array like,

["x", "y", "z", ..., "mach"]

The methods I've come up with so far are
embarrassingly hideous.

easy:

harp:~ > cat a.rb
a = 'variables="x", "y", "z", "rho", "u", "v", "w", "p/pinf", "s",
"mach"'

variables = eval a

p variables


harp:~ > ruby a.rb
["x", "y", "z", "rho", "u", "v", "w", "p/pinf", "s", "mach"]

As always, be wary of input to eval. Very wary:

a = 'variables="x", "y", "z", "rho", "u", "v", ""; puts "blackhat!";
x = "", "w", "p/pinf", "s", "mach"'
eval a

outputs

blackhat!

Tom Werner
 
W

William James

Bil said:
OK, I give up, what's the elegant method to
grab the quoted substrings in the following,

a = 'variables="x", "y", "z", "rho", "u", "v", "w", "p/pinf", "s", "mach"'

I want an array like,

["x", "y", "z", ..., "mach"]

The methods I've come up with so far are
embarrassingly hideous.

Thanks,

a = 'variables="x", "foo\"bar", "\"\"", "rho", "u", "p/pinf", "mach"'

x = a.scan( /"((?:\\.|.)*?)"/ ).flatten
p x
puts x

--- output -----
["x", "foo\\\"bar", "\\\"\\\"", "rho", "u", "p/pinf", "s", "mach"]
x
foo\"bar
\"\"
rho
u
p/pinf
s
mach
 
B

Bil Kleb

Bil said:
OK, I give up, what's the elegant method to
grab the quoted substrings in the following,

a = 'variables="x", "y", "z", "rho", "u", "v", "w", "p/pinf", "s", "mach"'

Thanks everyone!
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top