问题与how to extract a string from between two other strings完全相同,只是在其他两个字符串之间提取 所有 个字符串。
要使用与原始问题类似的示例,假设我们希望从字符串中提取GET_ME
和GET_ME_TOO
a <-" anything goes here, STR1 GET_ME STR2, anything goes here STR1 GET_ME_TOO STR2"
res <- str_match(a, "STR1 (.*?) STR2")
res[,2]
[1] "GET_ME"
这将检索第一次出现的信息,而不是第二次(或以后的)信息
我们如何在其他两个字符串之间提取 所有 个字符串?
答案 0 :(得分:3)
我们可以使用str_match_all
library(stringr)
str_match_all(a, "STR1 (.*?) STR2")
#[[1]]
# [,1] [,2]
#[1,] "STR1 GET_ME STR2" "GET_ME"
#[2,] "STR1 GET_ME_TOO STR2" "GET_ME_TOO"