Using this input:
x <- c("PP_Sample_12.GT", "PP_Sample-17.GT")
1) strsplit. Replace the first underscore with a dot and then split on dots:
spl <- strsplit(sub("_", ".", x), ".", fixed = TRUE)
sapply(spl, "[", 2)
2) gsub Replace the prefix (^[^_]*_
) and the suffix (\.[^.]*$"
) with the empty string:
gsub("^[^_]*_|\.[^.]*$", "", x)
3) gsubfn::strapplyc extract everything between underscore and dot.
library(gsubfn)
strapplyc(x, "_(.*)\.", simplify = TRUE)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…