Split quoted text

Hi!
Can anybody help me with following, it seems like I can find a solution that's shorter than 50LOC and I thought there must be a smarter way to do it...
My input would be something like "\this is\" \"my text\"" and I want to have an array containing ["\"this is\"", "\"my text\""].
I know it can't be that hard, but ...

Thanks for any reply!

Michael

something like this?
b=; t.scan(/(\".*?\")(?:\s*)/) { b << $1 }; p b
i'm sure there's something better though...
Alex

···

On Wed, Jul 14, 2004 at 09:27:03PM +0900, Michael Weller wrote:

Hi!
Can anybody help me with following, it seems like I can find a solution
that's shorter than 50LOC and I thought there must be a smarter way to
do it...
My input would be something like "\this is\" \"my text\"" and I want to
have an array containing ["\"this is\"", "\"my text\""].
I know it can't be that hard, but ...

Thanks for any reply!

Michael

mvg,
Alex

--
When women love us, they forgive us everything, even our crimes; when they do
not love us, they give us credit for nothing, not even our virtues.
    -- Honor'e de Balzac

Hi --

···

On Wed, 14 Jul 2004, Michael Weller wrote:

Hi!
Can anybody help me with following, it seems like I can find a solution
that's shorter than 50LOC and I thought there must be a smarter way to
do it...
My input would be something like "\this is\" \"my text\"" and I want to
have an array containing ["\"this is\"", "\"my text\""].
I know it can't be that hard, but ...

p '"this is" "my text"'.scan(/"[^"]+"/)
# => ["\"this is\"", "\"my text\""]

David

--
David A. Black
dblack@wobblini.net

Hi,

Michael Weller wrote:

Can anybody help me with following, it seems like I can find a solution that's shorter than 50LOC and I thought there must be a smarter way to do it...
My input would be something like "\this is\" \"my text\"" and I want to have an array containing ["\"this is\"", "\"my text\""].
I know it can't be that hard, but ...

Is quoting " needed?

0% ruby -rcsv -e 'p CSV.parse_line(%["this is" "my text"], ?\s)'
["this is", "my text"]

0% ruby -rcsv -e 'p CSV.parse(%["this is" "my text"|foo bar], ?\s, ?|)'
[["this is", "my text"], ["foo", "bar"]]

Regards,
// NaHi

David A. Black wrote:

Hi --

My input would be something like "\this is\" \"my text\"" and I want to have an array containing ["\"this is\"", "\"my text\""].
I know it can't be that hard, but ...
   
p '"this is" "my text"'.scan(/"[^"]+"/)
# => ["\"this is\"", "\"my text\""]

David

Yes, I think that's exactly what I searched... Thanks a lot!

Michael

···

On Wed, 14 Jul 2004, Michael Weller wrote:

"NAKAMURA, Hiroshi" <nahi@keynauts.com> schrieb im Newsbeitrag
news:40F542B0.1090707@keynauts.com...

Hi,

Michael Weller wrote:
> Can anybody help me with following, it seems like I can find a

solution

> that's shorter than 50LOC and I thought there must be a smarter way to
> do it...
> My input would be something like "\this is\" \"my text\"" and I want

to

> have an array containing ["\"this is\"", "\"my text\""].
> I know it can't be that hard, but ...

Is quoting " needed?

0% ruby -rcsv -e 'p CSV.parse_line(%["this is" "my text"], ?\s)'
["this is", "my text"]

0% ruby -rcsv -e 'p CSV.parse(%["this is" "my text"|foo bar], ?\s, ?|)'
[["this is", "my text"], ["foo", "bar"]]

Very nice! I usually use something like this for similar cases:

rx = %r{
  (?:"(?:[^\\"]|\\.)*")

(?:'(?:[^\\']|\\.)*')
\S+

}x

str.scan rx

Regards

    robert

David A. Black wrote:

Hi --

My input would be something like "\this is\" \"my text\"" and I want to have an array containing ["\"this is\"", "\"my text\""].
I know it can't be that hard, but ...

p '"this is" "my text"'.scan(/"[^"]+"/)
# => ["\"this is\"", "\"my text\""]

On that note, if you have escaped quotes you can use the following:
p '"he said \"hello\"" "field"'.scan(/"(?:\\.|[^"])*?"/)
# => ["\"he said \\\"hello\\\"\"", "\"field\""]

···

On Jul 14, 2004, at 5:51 AM, Michael Weller wrote:

On Wed, 14 Jul 2004, Michael Weller wrote:

David

Yes, I think that's exactly what I searched... Thanks a lot!

Michael

Charles Mills <cmills@freeshell.org> writes:

···

On Jul 14, 2004, at 5:51 AM, Michael Weller wrote:

David A. Black wrote:

Hi --

On Wed, 14 Jul 2004, Michael Weller wrote:

My input would be something like "\this is\" \"my text\"" and I want
to have an array containing ["\"this is\"", "\"my text\""].
I know it can't be that hard, but ...

p '"this is" "my text"'.scan(/"[^"]+"/)
# => ["\"this is\"", "\"my text\""]

On that note, if you have escaped quotes you can use the following:
p '"he said \"hello\"" "field"'.scan(/"(?:\\.|[^"])*?"/)
# => ["\"he said \\\"hello\\\"\"", "\"field\""]

There's also shellwords:

irb(main):001:0> require 'shellwords'
=> true
irb(main):002:0> Shellwords.shellwords('"he said \\"hello\\"" "field"')
=> ["he said \"hello\"", "field"]