3

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.

dimitris_ps
  • 5,849
  • 3
  • 29
  • 55
  • 2
    See a similar post [here](http://stackoverflow.com/questions/37811450/spliting-the-character-into-parts); modifying nicola's post a bit: `sapply(eval(parse(text = paste("alist(", s, ")", sep = ""))), deparse)` – alexis_laz Jun 19 '16 at 16:59

1 Answers1

5

Try

strsplit(s, "\\([^)]+\\)(*SKIP)(*FAIL)|, ", perl = TRUE)[[1]]
#[1] "abc"                    "xyz" 
#[3] "poi (cv, r2, 44, rghj)" "wer"        
akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    Thnx Arun. Do you know where i could read up on (*SKIP)(*FAIL)? Have never seen them – dimitris_ps Jun 19 '16 at 17:02
  • 1
    @dimitris_ps You can read more about it [here](http://stackoverflow.com/questions/24534782/how-do-skip-or-f-work-on-regex) – akrun Jun 19 '16 at 17:04