R 如何将字符串的部分捕获到向量中
我有以下字符串:R 如何将字符串的部分捕获到向量中,r,regex,stringr,R,Regex,Stringr,我有以下字符串: x <- "sim_K1000_human_compact" 您可以使用stringr::str\u match: str_match(x, "sim_K([0-9]+)_(\\w+)_(\\w+)")[,-1] # [1] "1000" "human" "compact" 下面是一个潜在的基本解决方案: x <- unlist(strsplit(gsub("sim_K", "", x), "_")) x我们可以使用scan和sub scan(tex
x <- "sim_K1000_human_compact"
您可以使用
stringr::str\u match
:
str_match(x, "sim_K([0-9]+)_(\\w+)_(\\w+)")[,-1]
# [1] "1000" "human" "compact"
下面是一个潜在的基本解决方案:
x <- unlist(strsplit(gsub("sim_K", "", x), "_"))
x我们可以使用scan
和sub
scan(text=sub("^[^_]+_.", "", x), what ="", quiet=TRUE, sep="_")
#[1] "1000" "human" "compact"
有点难看,但strsplit(“sim_K1000_human_compact”,“sim_K|||||”[[1]][-1]
strsplit(x,'\\w+[A-Z]||||)
scan(text=sub("^[^_]+_.", "", x), what ="", quiet=TRUE, sep="_")
#[1] "1000" "human" "compact"