stringr str_extract capture group capturing everything

后端 未结 3 986
无人共我
无人共我 2021-02-20 09:09

I\'m looking to extract the year from a string. This always comes after an \'X\' and before \".\" then a string of other characters.

Using stringr\'s

3条回答
  •  误落风尘
    2021-02-20 09:41

    The capture group is irrelevant in this case. The function str_extract will return the whole match including characters before and after the capture group.

    You have to work with lookbehind and lookahead instead. Their length is zero.

    library(stringr)
    str_extract(string = 'X2015.XML.Outgoing.pounds..millions.',
                pattern = '(?<=X)\\d{4}(?=\\.)')
    # [1] "2015"
    

    This regex matches four consecutive digits that are preceded by an X and followed by a ..

提交回复
热议问题