regex - R: how to find the first digit in a string -


    string = "abc3jfd456" 

suppose have above string, , wish find first digit in string , store value. in case, want store value 3 (since it's first-occuring digit in string). grepl("\\d", string) returns logical value, not tell me or first digit is. regular expression should use find value of first digit?

base r

regmatches(string, regexpr("\\d", string)) ## [1] "3" 

or using stringi

library(stringi) stri_extract_first(string, regex = "\\d") ## [1] "3" 

or using stringr

library(stringr) str_extract(string, "\\d") ## [1] "3" 

Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -