I have a long string. i want to sepearate a 10 digit phone no from it. eg "my no is 9876543210 is personal no and my official no is 123-456-8907. you can use any of these" i want to seperate the 9876543210 and 123-456-8907 from this. therev may be many phone nos in the string. how to do it ----- Thanks in Advance Arun -- View this message in context: http://r.789695.n4.nabble.com/searchina-a-pattern-in-a-string-tp4583740p4583740.html Sent from the R help mailing list archive at Nabble.com.
If it was only numbers with no dashes or up to 2 dashes, you can do it like this: require(stringr) STRING <- "my no is 9876543210 is personal no and my official no is 123-456-8907. you can use any of these" str_extract_all( STRING , "[0-9]+-*[0-9]+-*[0-9]+") (and then clean the numbers...) ----------------Contact Details:------------------------------------------------------- Contact me: Tal.Galili@gmail.com | 972-52-7275845 Read me: www.talgalili.com (Hebrew) | www.biostatistics.co.il (Hebrew) | www.r-statistics.com (English) ---------------------------------------------------------------------------------------------- On Tue, Apr 24, 2012 at 5:52 PM, arunkumar1111 <akpbond007@gmail.com> wrote:> I have a long string. i want to sepearate a 10 digit phone no from it. > > eg > > > "my no is 9876543210 is personal no and my official no is 123-456-8907. you > can use any of these" > > i want to seperate the 9876543210 and 123-456-8907 from this. therev may be > many phone nos in the string. how to do it > > ----- > Thanks in Advance > Arun > -- > View this message in context: > http://r.789695.n4.nabble.com/searchina-a-pattern-in-a-string-tp4583740p4583740.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
On Tue, Apr 24, 2012 at 10:52 AM, arunkumar1111 <akpbond007 at gmail.com> wrote:> I have a long string. i want to sepearate a 10 digit phone no from it. > > eg > > > "my no is 9876543210 is personal no and my official no is 123-456-8907. you > can use any of these" > > i want to seperate the 9876543210 and 123-456-8907 from this. therev may be > many phone nos in the string. how to do itTry this: library(gsubfn) pat <- "\\b\\d{3}-?\\d{3}-?\\d{4}\\b" strapply(x, pat, c, perl = TRUE, simplify = c) The last line gives this: [1] "9876543210" "123-456-8907" If its likely that any string of digits and minus signs which has 10 to 12 characters is a phone number then you could use this less accurate but shorter pattern: pat <- "[-0-9]{10,12}" -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com