【问题标题】:Extract all strings between two other strings?提取其他两个字符串之间的所有字符串?
【发布时间】:2020-01-27 19:37:51
【问题描述】:

Question 与how to extract a string from between two other strings 完全相同,只是在其他两个字符串之间提取所有字符串。

要使用与原始问题类似的示例,假设我们希望从字符串中提取GET_MEGET_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"

这会检索第一次,但不是第二次(或后续)出现

问题

我们如何在其他两个字符串之间提取所有字符串?

【问题讨论】:

    标签: r stringr


    【解决方案1】:

    我们可以使用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"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-21
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2015-05-05
      相关资源
      最近更新 更多