# Grabbing quoted substrings

**URL:** <https://www.ruby-forum.com/t/grabbing-quoted-substrings/79944>\
**Category:** Ruby\
**Created:** [December 20, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/grabbing-quoted-substrings/79944 "2006-12-20T18:52:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [December 20, 2006, 6:52pm UTC](https://www.ruby-forum.com/t/grabbing-quoted-substrings/79944/1 "2006-12-20T18:52:06Z")

</div>

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,

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [December 20, 2006, 7:00pm UTC](https://www.ruby-forum.com/t/grabbing-quoted-substrings/79944/2 "2006-12-20T19:00:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [December 20, 2006, 7:02pm UTC](https://www.ruby-forum.com/t/grabbing-quoted-substrings/79944/3 "2006-12-20T19:02:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [December 20, 2006, 7:31pm UTC](https://www.ruby-forum.com/t/grabbing-quoted-substrings/79944/4 "2006-12-20T19:31:08Z")

</div>

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](http://rubyforge.org/pipermail/bdrg-members/2006-June/000050.html)

cheers.

-a

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [December 20, 2006, 9:21pm UTC](https://www.ruby-forum.com/t/grabbing-quoted-substrings/79944/5 "2006-12-20T21:21:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bil\_K](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/848f3c/32.png) [@Bil\_K](https://www.ruby-forum.com/u/Bil_K)\
**Post date:** [December 20, 2006, 7:35pm UTC](https://www.ruby-forum.com/t/grabbing-quoted-substrings/79944/6 "2006-12-20T19:35:09Z")

</div>

removed\_email\_address@domain.invalid 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.
