[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Splitting A String

Andrew Stewart

3/16/2007 1:35:00 PM

Hello,

What's a (good) way to convert this:

'a quick "brown fox" jumped "over the lazy" dog'

into this:

[ 'a', 'quick', 'brown fox', 'jumped', 'over the lazy', 'dog' ] ?

Thanks!

Regards,
Andy Stewart

16 Answers

Guest

3/16/2007 1:47:00 PM

0

Andrew Stewart wrote:
> > What's a (good) way to convert this:
>
> 'a quick "brown fox" jumped "over the lazy" dog'
>
> into this:
>
> [ 'a', 'quick', 'brown fox', 'jumped', 'over the lazy', 'dog' ] ?


require 'csv'
CSV.parse_line('a quick "brown fox" jumped "over the lazy" dog', ' ')


regards
Jan

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

James Gray

3/16/2007 1:58:00 PM

0

On Mar 16, 2007, at 8:47 AM, Jan Friedrich wrote:

> Andrew Stewart wrote:
>>> What's a (good) way to convert this:
>>
>> 'a quick "brown fox" jumped "over the lazy" dog'
>>
>> into this:
>>
>> [ 'a', 'quick', 'brown fox', 'jumped', 'over the lazy', 'dog' ] ?
>
>
> require 'csv'
> CSV.parse_line('a quick "brown fox" jumped "over the lazy" dog', ' ')

Wow, that's mighty clever. I didn't even think of trying that. Nice
job.

James Edward Gray II

James Gray

3/16/2007 1:58:00 PM

0

On Mar 16, 2007, at 8:51 AM, Satish Talim wrote:

> On 3/16/07, Andrew Stewart <boss@airbladesoftware.com> wrote:
>>
>> Hello,
>>
>> What's a (good) way to convert this:
>>
>> 'a quick "brown fox" jumped "over the lazy" dog'
>>
>> into this:
>>
>> [ 'a', 'quick', 'brown fox', 'jumped', 'over the lazy', 'dog' ] ?
>>
>> Thanks!
>>
>> Regards,
>> Andy Stewart
>>
>
> Do this -
> 'a quick "brown fox" jumped "over the lazy" dog'.split

Not quite the same. Look again. ;)

James Edward Gray II

Andrew Stewart

3/16/2007 2:03:00 PM

0

Hello Jan,

On 16 Mar 2007, at 13:47, Jan Friedrich wrote:
> Andrew Stewart wrote:
>>> What's a (good) way to convert this:
>>
>> 'a quick "brown fox" jumped "over the lazy" dog'
>>
>> into this:
>>
>> [ 'a', 'quick', 'brown fox', 'jumped', 'over the lazy', 'dog' ] ?
>
>
> require 'csv'
> CSV.parse_line('a quick "brown fox" jumped "over the lazy" dog', ' ')

Nice!

Thank you!
Andy Stewart


Guest

3/16/2007 2:05:00 PM

0

Satish Talim wrote:
> 'a quick "brown fox" jumped "over the lazy" dog'.split
This was also my first idea, but

['a', 'quick', '"brown', 'fox"', 'jumped', '"over', 'the', 'lazy"',
'dog'] != ['a', 'quick', 'brown fox', 'jumped', 'over the lazy', 'dog']

regards
Jan

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

WoNáDo

3/16/2007 2:07:00 PM

0

James Edward Gray II schrieb:
> On Mar 16, 2007, at 8:51 AM, Satish Talim wrote:
>
>> On 3/16/07, Andrew Stewart <boss@airbladesoftware.com> wrote:
>>>
>>> Hello,
>>>
>>> What's a (good) way to convert this:
>>>
>>> 'a quick "brown fox" jumped "over the lazy" dog'
>>>
>>> into this:
>>>
>>> [ 'a', 'quick', 'brown fox', 'jumped', 'over the lazy', 'dog' ] ?
>>>
>>> Thanks!
>>>
>>> Regards,
>>> Andy Stewart
>>>
>>
>> Do this -
>> 'a quick "brown fox" jumped "over the lazy" dog'.split
>
> Not quite the same. Look again. ;)
>
> James Edward Gray II
>

But this works:

irb(main):001:0> 'a quick "brown fox" jumped "over the lazy" dog'.split(/[ "]+/)
=> ["a", "quick", "brown", "fox", "jumped", "over", "the", "lazy", "dog"]

Wolfgang Nádasi-Donner

Xavier Noria

3/16/2007 2:15:00 PM

0

On Mar 16, 2007, at 2:35 PM, Andrew Stewart wrote:

> Hello,
>
> What's a (good) way to convert this:
>
> 'a quick "brown fox" jumped "over the lazy" dog'
>
> into this:
>
> [ 'a', 'quick', 'brown fox', 'jumped', 'over the lazy', 'dog' ]

Can quotes be escaped? If not there's a simple regexp that does the job:

str = 'a quick "brown fox" jumped "over the lazy" dog'
puts str.scan(/"([^"]*)"|(\w+)/).flatten.select {|s| s}

You can also handle slashes, but it gets uglier.

-- fxn


Xavier Noria

3/16/2007 2:21:00 PM

0

On Mar 16, 2007, at 3:14 PM, Xavier Noria wrote:

> On Mar 16, 2007, at 2:35 PM, Andrew Stewart wrote:
>
>> Hello,
>>
>> What's a (good) way to convert this:
>>
>> 'a quick "brown fox" jumped "over the lazy" dog'
>>
>> into this:
>>
>> [ 'a', 'quick', 'brown fox', 'jumped', 'over the lazy', 'dog' ]
>
> Can quotes be escaped? If not there's a simple regexp that does the
> job:
>
> str = 'a quick "brown fox" jumped "over the lazy" dog'
> puts str.scan(/"([^"]*)"|(\w+)/).flatten.select {|s| s}

Heh, reading it I recalled there's a more specific idiom for that
last select:

str = 'a quick "brown fox" jumped "over the lazy" dog'
puts str.scan(/"((?:\\.|[^"])*)"|(\w+)/).flatten.compact

-- fxn


Logan Capaldo

3/16/2007 2:26:00 PM

0

On Fri, Mar 16, 2007 at 10:35:01PM +0900, Andrew Stewart wrote:
> Hello,
>
> What's a (good) way to convert this:
>
> 'a quick "brown fox" jumped "over the lazy" dog'
>
> into this:
>
> [ 'a', 'quick', 'brown fox', 'jumped', 'over the lazy', 'dog' ] ?
>
Here's yet another way:

#!/usr/bin/env ruby
require 'test/unit'
require 'strscan'
class TestScan < Test::Unit::TestCase
def test_splitter
assert_equal( %w(a b c), splitter(%q{a b c}))
assert_equal(["the", "\"quick brown\"", "fox", "jumped", "over", "the", "lazy", "dog"], splitter(%{the "quick brown" fox jumped over the lazy dog}))
end
end

def splitter(s)
res = []
scanner = StringScanner.new(s)
scanner.skip(/\s*/)
until scanner.eos?
if scanner.scan(/"/)
# quoted string
scanner.scan(/([^"]*")/)
res << '"' + scanner[1]
elsif scanner.scan(/(\S+)/)
res << scanner[1]
end
scanner.skip(/\s*/)
end
res
end
__END__
> Thanks!
>
> Regards,
> Andy Stewart

Xavier Noria

3/16/2007 2:27:00 PM

0

On Mar 16, 2007, at 3:21 PM, Xavier Noria wrote:

> puts str.scan(/"((?:\\.|[^"])*)"|(\w+)/).flatten.compact

Sorry, that regexp was part of a test and got copied by accident. I
just meant to clean up the select, that's:

str = 'a quick "brown fox" jumped "over the lazy" dog'
puts str.scan(/"([^"]*)"|(\w+)/).flatten.compact

-- fxn