And And - 1 year ago 79
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.

Answer Source

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

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

# [[1]]
# [1] ""
# [2] "_-iphone-se-64gb-spacegrau-apple.html" 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download