如果单词组不匹配,则按空格分割正则表达式。
如果匹配单词组,则保持原样。
text <- c('considerate and helpful','not bad at all','this is helpful')
pattern <- c('considerate and helpful','not bad')
输出: 体贴和乐于助人,不是坏事,所有,这个,是有帮助的
感谢您的帮助!
答案 0 :(得分:2)
当然,只需将这些字词放在\w+
:
library("stringr")
text <- c('considerate and helpful','not bad at all','this is helpful')
parts <- str_extract_all(text, "considerate and helpful|not bad|\\w+")
parts
哪个收益
[[1]]
[1] "considerate and helpful"
[[2]]
[1] "not bad" "at" "all"
[[3]]
[1] "this" "is" "helpful"
它不会在空格上分割,而是提取“单词”。