Extract part of string between two different patterns

前端 未结 4 1982
傲寒
傲寒 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 would use str_match from stringr: "str_match extracts capture groups formed by () from the first match. It returns a character matrix with one column for the complete match and one column for each group." ref

    str_match(my.string, paste(left.border, '(.+)', right.border, sep=''))[,2]
    

    The code above creates a regular expression with paste concatenating the capture group (.+) that captures 1 or more characters, with left and right borders (no spaces between strings).

    A single match is assumed. So, [,2] selects the second column from the matrix returned by str_match.

提交回复
热议问题