[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

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

Peter Bunyan

3/19/2008 7:54:00 AM

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...

Any better ideas? Is there already a built-in method to do this (or even
one in ActiveSupport)?
--
Posted via http://www.ruby-....

5 Answers

Mikel Lindsaar

3/19/2008 9:03:00 AM

0

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:

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://lin...

Sebastian Hungerecker

3/19/2008 10:26:00 AM

0

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

Peter Bunyan

3/19/2008 4:08:00 PM

0

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-....

Robert Klemme

3/19/2008 7:05:00 PM

0

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.

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

Andrew Stewart

3/20/2008 10:34:00 AM

0


On 19 Mar 2008, at 10:26, 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"]

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

-------
AirBlade Software
http://airbladeso...