Grabbing quoted substrings

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,

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

Bil K. wrote:

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

This should do the trick:

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

Tom W.

On Thu, 21 Dec 2006, Bil K. wrote:

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

Bil K. wrote:

embarrassingly hideous.

Thanks,

Bil K.
http://

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

[email protected] wrote:

[“x”, “y”, “z”, …, “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 W.