Extract part of string between two different patterns

前端 未结 4 1995
傲寒
傲寒 2020-12-30 15:04

I try to use stringr package to extract part of a string, which is between two particular patterns.

For example, I have:

my.string <         


        
4条回答
  •  清歌不尽
    2020-12-30 15:25

    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"
    

提交回复
热议问题