set.seed(1)
example <- paste0(
c("A","B")[sample(1:2,size = 100,replace = TRUE)],
sample(1:9999,100,replace=TRUE),
c("A","B","C")[sample(1:3,size = 100,replace = TRUE)],
sample(1:12,100,replace=TRUE)
)
strsplit(
sub(pattern = "^(A|B)([0-9]{1,4})(A|B|C)([0-9]{1,2})$",
replacement = "\\1 \\2 \\3 \\4",
x = example),
split = " ",
fixed = TRUE)
I want to do the same thing that I've done there, ie choosing some rigid regex groups and splitting between these groups.
But I want a one-line code in base R : can you do the same thing using only strsplit and and a regexp. That is, without adding delimiters and then splitting with theses delimiters.