より効率的な方法はありますか?なしでこれを行うにはどうすればよいstringr
ですか?
txt <- "I want to extract the words between this and that, this goes with that, this is a long way from that"
library(stringr)
w_start <- "this"
w_end <- "that"
pattern <- paste0(w_start, "(.*?)", w_end)
wordsbetween <- unlist(str_extract_all(txt, pattern))
gsub("^\\s+|\\s+$", "", str_sub(wordsbetween, nchar(w_start)+1, -nchar(w_end)-1))
[1] "and" "goes with" "is a long way from"