提取两个不同模式之间的部分字符串 [英] Extract part of string between two different patterns
本文介绍了提取两个不同模式之间的部分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用 stringr
包来提取字符串的一部分,它位于两个特定模式之间.
I try to use stringr
package to extract part of a string, which is between two particular patterns.
例如,我有:
my.string <- "nanaqwertybaba"
left.border <- "nana"
right.border <- "baba"
并通过使用 str_extract(string, pattern)
函数(其中模式是由 POSIX 正则表达式定义)我想收到:
and by the use of str_extract(string, pattern)
function (where pattern is defined by a POSIX regular expression) I would like to receive:
"qwerty"
来自 Google 的解决方案无效.
Solutions from Google did not work.
推荐答案
我不知道 stringr 提供的函数是否以及如何实现,但您也可以使用 base regexpr
和 substring
:
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"
这篇关于提取两个不同模式之间的部分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文