Split quoted text

M

Michael Weller

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
 
A

Alexander Kellett

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

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
 
D

David A. Black

Hi --

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
 
M

Michael Weller

David said:
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
 
N

NAKAMURA, Hiroshi

Hi,

Michael said:
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
 
C

Charles Mills

David said:
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\""]
 
R

Robert Klemme

NAKAMURA said:
Hi,

Michael said:
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
 
G

George Ogata

Charles Mills said:
David said:
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"]
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top