未列出str_,将所有str_定位到单独的开始和结束列表中

未列出str_,将所有str_定位到单独的开始和结束列表中,r,stringr,stringi,R,Stringr,Stringi,我使用str_locate_all获取字符串中模式列表的开始和结束位置。它返回一个列表,其中包含每个匹配的开始和停止位置。如何将所有比赛的开始和结束位置放入单独的列表中 library(stringr) patterns <- c("ABS", "BSDF", "ERIDF", "RTZOP") string <- "ABSBSDFERIDFRTZOPABSBSDFRTZOPABSBSDFERIDFRTZOP" matches <- str_locate_all(strin

我使用str_locate_all获取字符串中模式列表的开始和结束位置。它返回一个列表,其中包含每个匹配的开始和停止位置。如何将所有比赛的开始和结束位置放入单独的列表中

library(stringr)

patterns <- c("ABS", "BSDF", "ERIDF", "RTZOP")
string <- "ABSBSDFERIDFRTZOPABSBSDFRTZOPABSBSDFERIDFRTZOP"

matches <- str_locate_all(string, patterns)
我想要的是:

start <- c(1, 18, 30, 4, 21, 33, 8, 37, 13, 25, 42)
end <- c(3, 20, 32, 7, 24, 36, 12, 41, 17, 29, 46)

start使用do.call和rbind将列表堆叠在一起,然后取出所需的列

> library(stringr)
> 
> patterns <- c("ABS", "BSDF", "ERIDF", "RTZOP")
> string <- "ABSBSDFERIDFRTZOPABSBSDFRTZOPABSBSDFERIDFRTZOP"
> 
> matches <- str_locate_all(string, patterns)
> 
> all <- do.call(rbind, matches)
> start <- all[, 1]
> stop <- all[, 2]
> start
 [1]  1 18 30  4 21 33  8 37 13 25 42
> stop
 [1]  3 20 32  7 24 36 12 41 17 29 46
>库(stringr)
> 
>模式字符串
>火柴
>一切开始停止开始
[1]  1 18 30  4 21 33  8 37 13 25 42
>停止
[1]  3 20 32  7 24 36 12 41 17 29 46

工作正常!谢谢!
> library(stringr)
> 
> patterns <- c("ABS", "BSDF", "ERIDF", "RTZOP")
> string <- "ABSBSDFERIDFRTZOPABSBSDFRTZOPABSBSDFERIDFRTZOP"
> 
> matches <- str_locate_all(string, patterns)
> 
> all <- do.call(rbind, matches)
> start <- all[, 1]
> stop <- all[, 2]
> start
 [1]  1 18 30  4 21 33  8 37 13 25 42
> stop
 [1]  3 20 32  7 24 36 12 41 17 29 46