stringr str_extract capture group capturing everything

后端 未结 3 982
无人共我
无人共我 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:51

    Alternatively, you can use gsub:

    string = 'X2015.XML.Outgoing.pounds..millions.'
    
    gsub("X(\\d{4})\\..*", "\\1", string)
    # [1] "2015"
    

    or str_replace from stringr:

    library(stringr)
    str_replace(string, "X(\\d{4})\\..*", "\\1")
    # [1] "2015"
    

提交回复
热议问题