From: Xavier Noria Date: 2006-01-07T11:17:41+09:00 Subject: Re: Splitting strings on spaces, unless inside quotes On Jan 7, 2006, at 1:08, 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"] Curiously, someone asked exactly that on freenode#perl tonight. If the input is that simple and is assumed to be well-formed this is enough: irb(main):005:0> %q{some words "some quoted text" some "" more words}.scan(/"[^"]*"|\S+/) => ["some", "words", "\"some quoted text\"", "some", "\"\"", "more", "words"] Since nothing was said about this, it does not handle escaped quotes, and it assumes quotes are always balanced, so a field cannot be %q {"foo}, for example. -- fxn