Hi there I have a string like this i want to extract 9831019 from this string i used a regular expresion \d+ by which i can only make it to see 7 and returns. This type of number(9831019) appears in any part of the string and is definitely more than 5 digits all the time and i want to give that as a condition UV7C11-F9-E1 MCS#9831019 MCS Lot #9512516" how do i go abt it Ramya -- View this message in context: http://n4.nabble.com/Regular-expression-help-tp954834p954834.html Sent from the R help mailing list archive at Nabble.com.
On Dec 7, 2009, at 5:04 PM, Ramya wrote:> > Hi there > > I have a string like this i want to extract 9831019 from this string > i used > a regular expresion \d+ by which i can only make it to see 7 and > returns. > This type of number(9831019) appears in any part of the string and is > definitely more than 5 digits all the time and i want to give that > as a > condition > > UV7C11-F9-E1 MCS#9831019 > MCS Lot #9512516" > > > how do i go abt it > > RamyaIs the double quote actually part of your data or just a typo? I am not sure that it might matter in the end, but here is one approach: > x [1] "UV7C11-F9-E1 MCS#9831019" "MCS Lot #9512516\"" Note that I have the double quote included in the second value, which is escaped when printed here. > gsub("^.*#([0-9]*).*$", "\\1", x) [1] "9831019" "9512516" This uses gsub() to extract the value within the parens in the regex using a back reference. Any characters from the beginning of the line to the '#' are dropped, as are any characters after the numeric sequence to the end of the line. See ?gsub for more information. HTH, Marc Schwartz
Ramya - Try> strings = c('UV7C11-F9-E1 MCS#9831019','MCS Lot #9512516') > sub('^.*?(\\d{5,}).*?$','\\1',strings,perl=TRUE)[1] "9831019" "9512516" The regular expression finds the first string of five or more numbers in the strings. Since you said the numbers could occur anywhere in the string, you could have provided some examples where the numbers weren't the last part of the string. - Phil Spector Statistical Computing Facility Department of Statistics UC Berkeley spector at stat.berkeley.edu On Mon, 7 Dec 2009, Ramya wrote:> > Hi there > > I have a string like this i want to extract 9831019 from this string i used > a regular expresion \d+ by which i can only make it to see 7 and returns. > This type of number(9831019) appears in any part of the string and is > definitely more than 5 digits all the time and i want to give that as a > condition > > UV7C11-F9-E1 MCS#9831019 > MCS Lot #9512516" > > > how do i go abt it > > Ramya > -- > View this message in context: http://n4.nabble.com/Regular-expression-help-tp954834p954834.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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. >
If I understand correctly you wish to extract strings of digits more than 5 characters long: s <- c("UV7C11-F9-E1 MCS#9831019", "MCS Lot #9512516") library(gsubfn) strapply(s, "\\d{6,}", c) Depending on what you want to get back you might wish to add the simplify=TRUE argument to strapply, as well. See http://gsubfn.googlecode.com On Mon, Dec 7, 2009 at 6:04 PM, Ramya <ramya.victory at gmail.com> wrote:> > Hi ?there > > I have a string like this i want to extract 9831019 from this string i used > a regular expresion \d+ by which i can only make it to see 7 and returns. > This type of number(9831019) ?appears in any part of the string and is > definitely more than 5 digits all the time and i want to give that as a > condition > > UV7C11-F9-E1 MCS#9831019 > MCS Lot #9512516" > > > how do i go abt it > > Ramya > -- > View this message in context: http://n4.nabble.com/Regular-expression-help-tp954834p954834.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org 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. >