[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Grabbing quoted substrings

Bil Kleb

12/20/2006 5:47:00 PM

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,
--
Bil Kleb
http://
6 Answers

mitchell

12/20/2006 5:57:00 PM

0

Tom Werner

12/20/2006 5:59:00 PM

0

Bil Kleb wrote:
> 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

Ara.T.Howard

12/20/2006 6:08:00 PM

0

Tom Werner

12/20/2006 6:32:00 PM

0

ara.t.howard@noaa.gov wrote:
> On Thu, 21 Dec 2006, Bil Kleb wrote:
>
>> 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

William James

12/20/2006 8:15:00 PM

0

Bil Kleb wrote:
> 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,
> --
> Bil Kleb
> 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

Bil Kleb

12/20/2006 10:23:00 PM

0

Bil Kleb wrote:
> 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!

--
Bil Kleb

"I hate open source, you just can't get any support." -Bozo