Message-ID: <CAP01uRkHg04xjMfibUujovPVy+j0OxxrruUv9sPnz_RcgXRceg@mail.gmail.com>
Date: 2012-11-29T16:06:41Z
From: Gabor Grothendieck
Subject: splitting a string by space except when contained within quotes
In-Reply-To: <1354200226477-4651286.post@n4.nabble.com>
On Thu, Nov 29, 2012 at 9:43 AM, downtowater <downtowater at yahoo.ca> wrote:
> I've been trying to split a space delimited string with double-quotes in R
> for some time but without success. An example of a string is as follows:
>
> /rainfall snowfall "Channel storage" "Rivulet storage"/
>
> It's important for us because these are column headings that must match the
> subsequent data.
>
> Here is some code I've been trying:
>
> str <- 'rainfall snowfall "Channel storage" "Rivulet storage"'
> regex <- "[^\\s\"']+|\"([^\"]*)\""
> split <- strsplit(str, regex, perl=T)
> what I would like is
>
> [1] "rainfall" "snowfall" "Channel storage" "Rivulet storage"
>
> but what I get is:
>
> [1] "" " " " " " "
>
> The vector is the right length (which is encouraging) but of course the
> strings are empty or contain a single space. Any suggestions?
Try this:
> scan(con <- textConnection(str), what = "")
Read 4 items
[1] "rainfall" "snowfall" "Channel storage" "Rivulet storage"
> close(con)
email: ggrothendieck at gmail.com