Hi I have this problem where I need to find if there is any numbers in a string, this is no problem if theres only one number per string. I would then simply use the regexpr() funtion togheter with the substring function to extract the number. But regexpr only picks one number per string either from the beginning or the end, but not multiple. Can this be done? And how for example My string <- "this1is2a3test" The result I want is an vector of c(1,2,3) //Tom -- View this message in context: http://www.nabble.com/Finding-multiple-characters-in-the-same-string-tf4205206.html#a11961686 Sent from the R help mailing list archive at Nabble.com.
> gregexpr("[0-9]","this1is2a3test")[[1]] [1] 5 8 10 attr(,"match.length") [1] 1 1 1> unlist(gregexpr("[0-9]","this1is2a3test"))[1] 5 8 10 Tom.O wrote:> > I have this problem where I need to find if there is any numbers in a > string, this is no problem if theres only one number per string. I would > then simply use the regexpr() funtion togheter with the substring function > to extract the number. But regexpr only picks one number per string either > from the beginning or the end, but not multiple. Can this be done? And how > > for example > My string <- "this1is2a3test" > > The result I want is an vector of c(1,2,3) >-- View this message in context: http://www.nabble.com/Finding-multiple-characters-in-the-same-string-tf4205206.html#a11962341 Sent from the R help mailing list archive at Nabble.com.
Gabor Grothendieck
2007-Aug-02 11:07 UTC
[R] Finding multiple characters in the same string
Here are a few different approaches: s <- "this1is2a3test44of extraction" tmp <- gsub("[^[:digit:]]", " ", s) scan(textConnection(tmp), what = 0) tmp <- gsub("[^[:digit:]]", " ", s) spl <- strsplit(tmp, " ")[[1]] as.numeric(spl[spl != ""]) library(gsubfn) strapply(s, "[[:digit:]]+", as.numeric)[[1]] On 8/2/07, Tom.O <tom.olsson at dnbnor.com> wrote:> > Hi > I have this problem where I need to find if there is any numbers in a > string, this is no problem if theres only one number per string. I would > then simply use the regexpr() funtion togheter with the substring function > to exclude the number. But regexpr only picks one number per string either > from the beginning or the end, but not multiple. Can this be done? And how > > for example > My string <- "this1is2a3test" > > The result I want is an vector of c(1,2,3) > > //Tom > -- > View this message in context: http://www.nabble.com/Finding-multiple-characters-in-the-same-string-tf4205206.html#a11961686 > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >
Thanks for the quick reply //Tom Vladimir Eremeev wrote:> >> gregexpr("[0-9]","this1is2a3test") > [[1]] > [1] 5 8 10 > attr(,"match.length") > [1] 1 1 1 > >> unlist(gregexpr("[0-9]","this1is2a3test")) > [1] 5 8 10 > > > Tom.O wrote: >> >> I have this problem where I need to find if there is any numbers in a >> string, this is no problem if theres only one number per string. I would >> then simply use the regexpr() funtion togheter with the substring >> function to extract the number. But regexpr only picks one number per >> string either from the beginning or the end, but not multiple. Can this >> be done? And how >> >> for example >> My string <- "this1is2a3test" >> >> The result I want is an vector of c(1,2,3) >> > >-- View this message in context: http://www.nabble.com/Finding-multiple-characters-in-the-same-string-tf4205206.html#a11962577 Sent from the R help mailing list archive at Nabble.com.
grep("[0-9]",unlist(strsplit("this1is2a3test","")),value=TRUE) Tom.O wrote:> > I have this problem where I need to find if there is any numbers in a > string, this is no problem if theres only one number per string. I would > then simply use the regexpr() funtion togheter with the substring function > to extract the number. But regexpr only picks one number per string either > from the beginning or the end, but not multiple. Can this be done? And how > > for example > My string <- "this1is2a3test" > > The result I want is an vector of c(1,2,3) >-- View this message in context: http://www.nabble.com/Finding-multiple-characters-in-the-same-string-tf4205206.html#a11962578 Sent from the R help mailing list archive at Nabble.com.