Extract part of string between two different patterns

前端 未结 4 1958
傲寒
傲寒 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:26

    In base R you can use gsub. The parentheses in the pattern create numbered capturing groups. Here we select the second group in the replacement, i.e. the group between the borders. The . matches any character. The * means that there is zero or more of the preceeding element

    gsub(pattern = "(.*nana)(.*)(baba.*)",
         replacement = "\\2",
         x = "xxxnanaRisnicebabayyy")
    # "Risnice"
    

提交回复
热议问题