1) sub
sub(".*_(\\d{5})_.*", "\\1", x) ## [1] "00691"
2) gsubfn :: strapplyc The regular expression can be slightly simplified if we use strapplyc :
library(gsubfn) strapplyc(x, "_(\\d{5})_", simplify = TRUE) ## [1] "00691"
3) strsplit If we know that this is the third field:
read.table(text = x, sep = "_", colClasses = "character")$V3
3a) or
strsplit(x, "_")[[1]][3] ## [1] "00691"
source share