Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Split quoted text

Reply
Thread Tools

Split quoted text

 
 
Michael Weller
Guest
Posts: n/a
 
      07-14-2004
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



 
Reply With Quote
 
 
 
 
Alexander Kellett
Guest
Posts: n/a
 
      07-14-2004
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



 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      07-14-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)



 
Reply With Quote
 
Michael Weller
Guest
Posts: n/a
 
      07-14-2004
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\""]
>
>
>David
>
>
>

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

Michael


 
Reply With Quote
 
NAKAMURA, Hiroshi
Guest
Posts: n/a
 
      07-14-2004
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


 
Reply With Quote
 
Charles Mills
Guest
Posts: n/a
 
      07-14-2004

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

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




 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      07-14-2004

"NAKAMURA, Hiroshi" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed)...
> 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

 
Reply With Quote
 
George Ogata
Guest
Posts: n/a
 
      07-15-2004
Charles Mills <(E-Mail Removed)> 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"]
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regex help: Split string into words AND double-quoted phrases Robert Oschler Javascript 2 08-02-2005 11:01 AM
split a string with quoted parts into list oliver Python 5 03-11-2005 05:31 AM
Thunderbird and quoted text Matt O'Toole Firefox 1 11-13-2004 07:01 PM
Does 1.5 RC2 have the option of "start my reply above the quoted text"? Cameron Firefox 3 10-08-2003 11:44 AM
split operator with quoted strings arasu@nospam.org Perl Misc 2 07-11-2003 12:43 PM



Advertisments