r - Split character string by forward slash or nothing


Keywords:r 


Question: 

I want to split this vector

c("CC", "C/C")

to

[[1]]
[1] "C" "C"

[[2]]
[1] "C" "C"

My final data should look like:

c("C_C", "C_C")

Thus, I need some regex, but don't found how to solve the "non-space" part:

strsplit(c("CC", "C/C"),"|/")

3 Answers: 

You can use sub (or gsub if it occurs more than once in your string) to directly replace either nothing or a forward slash with an underscore (capturing one character words around):

sub("(\\w)(|/)(\\w)", "\\1_\\3", c("CC", "C/C"))
#[1] "C_C" "C_C"
 

We can split the string at every character, omit the "/" and paste them together.

sapply(strsplit(x, ""), function(v) paste0(v[v!= "/"], collapse = "_"))

#[1] "C_C" "C_C"

data

x <- c("CC", "C/C")
 

We can use

lapply(strsplit(v1, "/|"), function(x) x[nzchar(x)])

Or use a regex lookaround

strsplit(v1, "(?<=[^/])(/|)", perl = TRUE)
#[[1]]
#[1] "C" "C"

#[[2]]
#[1] "C" "C"

If the final output should be a vector, then

gsub("(?<=[^/])(/|)(?=[^/])", "_", v1, perl = TRUE)
#[1] "C_C" "C_C"