# Parsing a string with quotes

**URL:** <https://www.ruby-forum.com/t/parsing-a-string-with-quotes/127609>\
**Category:** Ruby\
**Created:** [January 30, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/parsing-a-string-with-quotes/127609 "2008-01-30T20:28:50Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![liquidrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@liquidrails](https://www.ruby-forum.com/u/liquidrails)\
**Post date:** [January 30, 2008, 8:28pm UTC](https://www.ruby-forum.com/t/parsing-a-string-with-quotes/127609/1 "2008-01-30T20:28:50Z")

</div>

I am trying to parse out the string:

My dog “ate” my “math homework” and “my cat”

into the array:

[My, dog, ate, my, math homework, and, my cat]

Does anybody know a good way to do this?

Thank you in advance,

Cheri

---

<div class="post-metadata">

**Author:** ![liquidrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@liquidrails](https://www.ruby-forum.com/u/liquidrails)\
**Post date:** [January 30, 2008, 8:42pm UTC](https://www.ruby-forum.com/t/parsing-a-string-with-quotes/127609/2 "2008-01-30T20:42:17Z")

</div>

On Jan 30, 12:28 pm, Cheri R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to parse out the string:
> 
> My dog “ate” my “math homework” and “my cat”
> 
> into the array:
> 
> [My, dog, ate, my, math homework, and, my cat]

irb(main):001:0\> str = ‘My dog “ate” my “math homework” and “my cat”’  
=\> “My dog "ate" my "math homework" and "my cat"”  
irb(main):002:0\> str.scan( /“[^”]+“|\w+/ ).map{ |w| w.gsub '”', ‘’ }  
=\> [“My”, “dog”, “ate”, “my”, “math homework”, “and”, “my cat”]

Not a very robust solution. Won’t handle a " in the middle of some  
quotes, or an incorrectly unpaired quote.

---

<div class="post-metadata">

**Author:** ![liquidrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@liquidrails](https://www.ruby-forum.com/u/liquidrails)\
**Post date:** [January 30, 2008, 8:44pm UTC](https://www.ruby-forum.com/t/parsing-a-string-with-quotes/127609/3 "2008-01-30T20:44:03Z")

</div>

On Jan 30, 2008, at 12:28 PM, Cheri R. wrote:

> My dog “ate” my “math homework” and “my cat”

cfp2:~ \> cat a.rb  
string = ‘My dog “ate” my “math homework” and “my cat”’

re = %r/[“'][^”‘]+["’]|\w+/

tokens = string.scan re

puts tokens

cfp2:~ \> ruby a.rb  
My  
dog  
“ate”  
my  
“math homework”  
and  
“my cat”

a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![liquidrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@liquidrails](https://www.ruby-forum.com/u/liquidrails)\
**Post date:** [January 30, 2008, 8:45pm UTC](https://www.ruby-forum.com/t/parsing-a-string-with-quotes/127609/4 "2008-01-30T20:45:49Z")

</div>

On Jan 30, 2008, at 2:28 PM, Cheri R. wrote:

> I am trying to parse out the string:
> 
> My dog “ate” my “math homework” and “my cat”
> 
> into the array:
> 
> [My, dog, ate, my, math homework, and, my cat]
> 
> Does anybody know a good way to do this?

require “shellwords”  
string = ‘My dog “ate” my “math homework” and “my cat”’  
array = Shellwords.shellwords string  
p array

HTH

---

<div class="post-metadata">

**Author:** ![liquidrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@liquidrails](https://www.ruby-forum.com/u/liquidrails)\
**Post date:** [January 30, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/parsing-a-string-with-quotes/127609/5 "2008-01-30T20:54:14Z")

</div>

Thank you for your help. Where is a good resource to read up on what  
this code means? %r/[“'][^”‘]+["’]|\w+/

Thanks, Cheri

ara howard wrote:

> On Jan 30, 2008, at 12:28 PM, Cheri R. wrote:
> 
> > My dog “ate” my “math homework” and “my cat”
> 
> cfp2:~ \> cat a.rb  
> string = ‘My dog “ate” my “math homework” and “my cat”’
> 
> re = %r/[“'][^”‘]+["’]|\w+/
> 
> tokens = string.scan re
> 
> puts tokens
> 
> cfp2:~ \> ruby a.rb  
> My  
> dog  
> “ate”  
> my  
> “math homework”  
> and  
> “my cat”
> 
> a @ [http://codeforpeople.com/](http://codeforpeople.com/)

---

<div class="post-metadata">

**Author:** ![liquidrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@liquidrails](https://www.ruby-forum.com/u/liquidrails)\
**Post date:** [January 30, 2008, 8:54pm UTC](https://www.ruby-forum.com/t/parsing-a-string-with-quotes/127609/6 "2008-01-30T20:54:45Z")

</div>

On Jan 30, 2008 2:28 PM, Cheri R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you in advance,
> 
> Cheri

irb(main):001:0\> require ‘rubygems’  
=\> false  
irb(main):002:0\> require ‘faster\_csv’  
=\> true  
irb(main):003:0\> arr = FasterCSV.parse\_line( ‘My dog “ate” my “math  
homework” and “my cat”’, {:col\_sep =\> ’ '})  
=\> [“My”, “dog”, “ate”, “my”, “math homework”, “and”, “my cat”]

---

<div class="post-metadata">

**Author:** ![liquidrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@liquidrails](https://www.ruby-forum.com/u/liquidrails)\
**Post date:** [January 30, 2008, 9:50pm UTC](https://www.ruby-forum.com/t/parsing-a-string-with-quotes/127609/7 "2008-01-30T21:50:19Z")

</div>

Cheri R. wrote:

> Thank you for your help. Where is a good resource to read up on what  
> this code means? %r/["’][^"’]+["’]|\w+/

The topic is called ‘regular expressions’. That regular expression says  
to find a matching substring where:

1. The first character is either a single or double quote: [’"]  
Brackets allow you to specify a group of characters, e.g. [a-z]. If the  
first character in a matching substring is any of the characters in the  
specified group, then there is a match.

2. Followed by a character that is not(^) one of the characters in the  
specified group: [^’"]  
If the first character between brackets is ^, it means look for any  
character not specified between the brackets.

3. The + after the brackets says to look for “one or more” of the  
preceding matching character, i.e. not a single or double quote.

4. Followed by a single or double quote: ["’]

5. The pipe(|) means OR. So a substring will also match if the symbols  
following the pipe match.

6. \w means any ‘word character’, which is short hand for: [a-zA-Z0-9]  
Once again the + sign following the \w means to match the \w one or more  
times.

The result is that the regex matches any phrase surrounded by single or  
double quotes OR any series of characters [a-zA-z0-9]. Note that a  
space will not match \w so when a space is encountered, the match ends.

---

<div class="post-metadata">

**Author:** ![liquidrails](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/a6a055/32.png) [@liquidrails](https://www.ruby-forum.com/u/liquidrails)\
**Post date:** [January 30, 2008, 10:00pm UTC](https://www.ruby-forum.com/t/parsing-a-string-with-quotes/127609/8 "2008-01-30T22:00:53Z")

</div>

On Jan 30, 12:54 pm, Cheri R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thank you for your help. Where is a good resource to read up on what  
> this code means? %r/[“'][^”‘]+["’]|\w+/

Intro:  
[http://phrogz.net/ProgrammingRuby/tut\_stdtypes.html#regularexpressions](http://phrogz.net/ProgrammingRuby/tut_stdtypes.html#regularexpressions)

Deep Info:  
[http://phrogz.net/ProgrammingRuby/language.html#regularexpressions](http://phrogz.net/ProgrammingRuby/language.html#regularexpressions)
