dimitris_ps
dimitris_ps

Reputation: 5951

Split string according to commas in R

I have the following:

s <- "abc, xyz, poi (cv, r2, 44, rghj), wer"

How can I split it so the end result is:

c("abc", "xyz", "poi (cv, r2, 44, rghj)", "wer")

Basically, strsplit the string at every comma, but outside the parentheses.

Upvotes: 3

Views: 118

Answers (1)

akrun
akrun

Reputation: 886948

Try

strsplit(s, "\\([^)]+\\)(*SKIP)(*FAIL)|, ", perl = TRUE)[[1]]
#[1] "abc"                    "xyz" 
#[3] "poi (cv, r2, 44, rghj)" "wer"        

Upvotes: 5

Related Questions