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
stringr
Alternatively, you can use gsub:
gsub
string = 'X2015.XML.Outgoing.pounds..millions.' gsub("X(\\d{4})\\..*", "\\1", string) # [1] "2015"
or str_replace from stringr:
str_replace
library(stringr) str_replace(string, "X(\\d{4})\\..*", "\\1") # [1] "2015"