R 中的正则表达式匹配一组单词并按 space 拆分

Regex in R to match a group of words and split by space

如果单词组不匹配,则将正则表达式拆分为 space。
如果一组单词匹配,则保持原样。

text <-  c('considerate and helpful','not bad at all','this is helpful') 
pattern <- c('considerate and helpful','not bad')

输出: 体贴,乐于助人,还不错,都,这个,是,有帮助

感谢您的帮助!

当然,只要把\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"

它不会按空格拆分,而是提取 "words"。