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

P

Peter Bunyan

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)?
 
M

Mikel Lindsaar

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:

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
http://lindsaar.net/
 
S

Sebastian Hungerecker

Peter said:
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
 
P

Peter Bunyan

Sebastian said:
Peter said:
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.
 
R

Robert Klemme

Sebastian said:
Peter said:
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.

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
 
A

Andrew Stewart

Peter said:
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=> ["the", "quick brown", "fox", "jumped", "over a lazy", "dog"]

Unfortunately it doesn't handle the pesky backslash in Sebastian's
example above.
=> []

Oh well!

Cheers,
Andy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top