And And - 6 months ago 27
R Question

Split the character vector into two parts

Consider the following character vector of the length 1:

l <- ""

I desire to split it into two parts, so that the first part should be:

p1 <- ""

and the second one:

p2 <- "_-iphone-se-64gb-spacegrau-apple.html"

Surely, one must use regexp to solve the problem. Please could you give me some insight where I can learn manipulation with regular expressions easily. For any help I will be sincerely thankful.


Use "(?<=[^_])(?=_)" with strsplit gives you what you need:

strsplit(l, "(?<=[^_])(?=_)", perl = T)

# [[1]]
# [1] ""
# [2] "_-iphone-se-64gb-spacegrau-apple.html"