From: Geoff Jacobsen Date: 2006-01-10T10:50:07+09:00 Subject: Re: Splitting strings on spaces, unless inside quotes On Tue, 2006-01-10 at 04:23 +0900, William James wrote: > Geoff Jacobsen wrote: > > On Mon, 2006-01-09 at 18:13 +0900, William James wrote: > > > Richard Livsey wrote: > > > > I want to split a string into words, but group quoted words together > > > > such that... > > > > > > > > some words "some quoted text" some more words > > > > > > > > would get split up into: > > > > > > > > ["some", "words", "some quoted text", "some", "more", "words"] > > > > > > s = 'some words "some quoted text" some more words' > > > p s.split( / *"(.*?)" *| / ) > > > > Which along with the CSV solution can't handle complex cases: > > > > s='one two" "\'with quotes\' "three "' > > > > s.split( / *"(.*?)" *| / ) > > => ["one", "two", " ", "'with", "quotes'", "three "] ... > > but Shellwords can: > > > > require 'shellwords' > > Shellwords.shellwords(s) > > => ["one", "two with quotes", "three "] > > This is not a "more complex case"; it is an invalid case. > The original poster simply wanted to avoid splitting on spaces > within double quotes, not within single quotes. > > The shellwords "solution" is a solution to a different problem, not > to this one. It can't even handle a simple case: > > require 'shellwords' > s = "why can't you think?" > Shellwords.shellwords(s) > > ArgumentError: Unmatched single quote: 't you think? > I agree my example doesn't match the originators request but *I think* there is enough ambiguity about the post to postulate that they may want more real-world cases such as: s='symbol "William said: \"why can't you think?\"" 123 "foo"' Shellwords.shellwords(s) => ["symbol", "William said: \"why can't you think?\"", "123", "foo"] So Shellwords may indeed be a solution to this problem but the problem is not stated precisely enough to know.