如何将一个字符串多于一个空格拆分为R中的列表元素?

时间:2019-06-29 23:36:59

标签: r regex stringr

我需要将一个以上有多个空格的字符串拆分为列表元素。假设我有以下字符串:

"My name     is Bob and   I like programming."

我尝试了以下代码:

library(stringr)
string <- "My name     is Bob and   I like programming."
strsplit(string,"\\s+")

哪个给出结果:

[[1]]
[1] "My"           "name"         "is"          
[4] "Bob"          "and"          "I"           
[7] "like"         "programming."

但是,我想要这个结果:

[[1]]
[1] My name

[[2]]
[1] is Bob and 

[[3]]
[1] I like programming.

感谢您的帮助!

0 个答案:

没有答案