Extract a regular expression match
It is probably a bit hasty to say 'ignore the standard functions' - the help file for ?gsub
even specifically references in 'See also':
‘regmatches’ for extracting matched substrings based on the results of ‘regexpr’, ‘gregexpr’ and ‘regexec’.
So this will work, and is fairly simple:
txt <- "aaa12xxx"regmatches(txt,regexpr("[0-9]+",txt))#[1] "12"