根据R中的字符串向量拆分字符串

Splitting a string based on a vector of strings in R

我有以下字符串和向量:

temp = "EarthMars Venus & Saturn PlanetsJupiter"

searchTerms = c("Earth", "Jupiter", "Mars", "Venus & Saturn Planets", "Neptune")

我想根据 'searchTerms' 中的字符串拆分 'temp',以便得到以下内容:

result = c("Earth", "Mars", "Venus & Saturn Planets", "Jupiter") 

感谢您的帮助!

一个与此类似的选项 post (R split on delimiter (split) keep the delimiter (split)) 是:

searchStr <- paste0(searchTerms, collapse = "|")
unlist(strsplit(temp, paste0("(?<=",searchStr,")"), perl = T))

[1] "Earth" "Mars"  " Venus & Saturn Planets" "Jupiter"   

使用 stringr 包,您可以:

library(stringr)
result = unlist(str_extract_all(temp,searchTerms))

[1] "Earth"          "Jupiter"         "Mars"           "Venus & Saturn Planets"

另一个只有基本函数的单行选项:

result <- unlist(lapply(searchTerms, function(x) regmatches(temp,regexpr(x,temp))))