Split string at spaces, but not when inside quotation marks?

I want to be able to split a string at the space, unless the spaces are
inside question marks. I've got a solution, but it makes baby Jesus cry:
http://pastebin.com/m6db29d1e

Any better ideas? Is there already a built-in method to do this (or even
one in ActiveSupport)?

···

--
Posted via http://www.ruby-forum.com/.

string = 'This is one solution "that uses a while loop" but I think
"someone can do it with map"'
quotes =
while string =~ /".*?"/
  quotes << string.slice!(/".*?"/)
end
p quotes + string.split(' ')

#=> ["\"that uses a while loop\"", "\"someone can do it with map\"",
"This", "is", "one", "solution", "but", "I", "think"]

Mikel

···

On Wed, Mar 19, 2008 at 6:53 PM, Peter Bunyan <peter.bunyan@gmail.com> wrote:

I want to be able to split a string at the space, unless the spaces are
inside question marks. I've got a solution, but it makes baby Jesus cry:

# I want to be able to split a string at the space, unless the
# spaces are
# inside question marks. I've got a solution, but it makes baby

       ^^^^^^^^^^^ i think you meant quotation marks, no?

# Jesus cry:
# http://pastebin.com/m6db29d1e

that is one way and it does not look bad.
there are many ways, of course.

# Any better ideas?

im not sure if this is better, but

irb(main):071:0> x
=> "this \"this too \" not this \"these too \" not these "

irb(main):072:0> x.split(/\s*?(".*?")\s*?/).map{|x| x=~/^".*"$/ ? x : x.split}.flatten
=> ["this", "\"this too \"", "not", "this", "\"these too \"", "not", "these"]

# Is there already a built-in method to do
# this (or even one in ActiveSupport)?

i do not know of ActiveSupport in ruby. Are you talking about rails?

kind regards -botp

···

From: Peter Bunyan [mailto:peter.bunyan@gmail.com]

Peter Bunyan wrote:

Is there already a built-in method to do this

require 'shellwords'
Shellwords.shellwords 'Hello "wo rld" how "are \" you today"'
=> ["Hello", "wo rld", "how", "are \" you today"]

HTH,
Sebastian

···

--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Sebastian Hungerecker wrote:

Peter Bunyan wrote:

Is there already a built-in method to do this

require 'shellwords'
Shellwords.shellwords 'Hello "wo rld" how "are \" you today"'
=> ["Hello", "wo rld", "how", "are \" you today"]

HTH,
Sebastian

Mmm, that's yummy! Thanks, I'm using this now. Thanks to all of your
replies, you all rock hard.

···

--
Posted via http://www.ruby-forum.com/\.

Peter Bunyan wrote:

Is there already a built-in method to do this

require 'shellwords'
Shellwords.shellwords 'Hello "wo rld" how "are \" you today"'
=> ["Hello", "wo rld", "how", "are \" you today"]

Or you can pretend it's CSV:

$ irb
>> require 'csv'
=> true
>> s = 'the "quick brown" fox jumped "over a lazy" dog'
=> "the "quick brown" fox jumped "over a lazy" dog"
>> CSV.parse_line s, ' '
=> ["the", "quick brown", "fox", "jumped", "over a lazy", "dog"]

Unfortunately it doesn't handle the pesky backslash in Sebastian's example above.

>> s = 'Hello "wo rld" how "are \" you today"'
=> "Hello "wo rld" how "are \\" you today""
>> CSV.parse_line s, ' '
=>

Oh well!

Cheers,
Andy

···

On 19 Mar 2008, at 10:26, Sebastian Hungerecker wrote:

-------
AirBlade Software

Though I'm coming in late to the party: sometimes you can exchange #split and #scan. This is something I use sometimes:

irb(main):006:0> string = 'a simple solution that uses "a regular expression" - see?'
=> "a simple solution that uses \"a regular expression\" - see?"
irb(main):007:0> quotes = string.scan %r{"[^"]*"|\S+}
=> ["a", "simple", "solution", "that", "uses", "\"a regular expression\"", "-", "see?"]

Note: the order of the alternative in the regexp matters! This works because Ruby's regex engine is NFA based. Here's what happens if you exchange the two

irb(main):008:0> quotes = string.scan %r{\S+|"[^"]*"}
=> ["a", "simple", "solution", "that", "uses", "\"a", "regular", "expression\"", "-", "see?"]

Kind regards

  robert

···

On 19.03.2008 17:07, Peter Bunyan wrote:

Sebastian Hungerecker wrote:

Peter Bunyan wrote:

Is there already a built-in method to do this

require 'shellwords'
Shellwords.shellwords 'Hello "wo rld" how "are \" you today"'
=> ["Hello", "wo rld", "how", "are \" you today"]

HTH,
Sebastian

Mmm, that's yummy! Thanks, I'm using this now. Thanks to all of your replies, you all rock hard.