I have different character vector containing strings like "p.L86*"
, "p.A59fs*4"
, "p.E309*"
, etc. Each have different digits. I only want to extract the first a few numbers between the characters, so the expected solution would be 86, 59, 309
.
I tried gsub("[^0-9]+","","p.A59fs*4")
, but it will save all digits...
You can use sub
to get the first match results:
x <- c('p.L86*', 'p.A59fs*4', 'p.E309*')
sub('\\D*(\\d+).*', '\\1', x)
# [1] "86" "59" "309"
Or fallback to the stringi package and match them instead:
stri_extract_first_regex(x, '\\d+')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With