且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

提取两个不同模式之间的部分字符串

更新时间:2023-02-21 18:55:43

我不知道 stringr 提供的函数是否以及如何实现,但您也可以使用 base regexprsubstring:

I do not know whether and how this is possible with functions provided by stringr but you can also use base regexpr and substring:

pattern <- paste0("(?<=", left.border, ")[a-z]+(?=", right.border, ")")
# "(?<=nana)[a-z]+(?=baba)"

rx <- regexpr(pattern, text=my.string, perl=TRUE)
# [1] 5
# attr(,"match.length")
# [1] 6

substring(my.string, rx, rx+attr(rx, "match.length")-1)
# [1] "qwerty"