Waverley @ Palo Alto
2010-Aug-22 22:05 UTC
[R] how to implement string pattern extraction in R
Hi, In perl, to get a substring matching a particular pattern can be implemented like the following example: $x = "AAAA.txt"; if ($x=~ /(.*?)\.txt/){ $prefix = $1; } So how to do the same thing in R? Can someone provide me the code sample? Thanks much in advance. -- Waverley @ Palo Alto
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] > On Behalf Of Waverley @ Palo Alto > Sent: Sunday, August 22, 2010 3:05 PM > To: r-help > Subject: [R] how to implement string pattern extraction in R > > Hi, > > In perl, to get a substring matching a particular pattern can be > implemented like the following example: > > $x = "AAAA.txt"; > if ($x=~ /(.*?)\.txt/){ > $prefix = $1; > } > > So how to do the same thing in R? > > Can someone provide me the code sample? > > Thanks much in advance. > > -- > Waverley @ Palo Alto >Check out the documentation for regexp ?regexp Hope this is helpful, Dan Daniel Nordlund Bothell, WA USA
Hi Waverley, I am not familiar at all with perl, but this should get you headed in the right direction: #Some documentation that might help ?regexpr ?grep ?regexp #I imagine you sould do something like x <- "AAAA.txt" regexpr(pattern = "yourpattern", text = x) grep(pattern = "yourpattern", x = x) #also note the "perl =" argument you can set in regexpr() Cheers, Josh On Sun, Aug 22, 2010 at 3:05 PM, Waverley @ Palo Alto <waverley.paloalto at gmail.com> wrote:> Hi, > > In perl, to get a substring matching a particular ?pattern can be > implemented like the following example: > > $x = "AAAA.txt"; > if ($x=~ /(.*?)\.txt/){ > ?$prefix = $1; > } > > So how to do the same thing in R? > > Can someone provide me the code sample? > > Thanks much in advance. > > -- > Waverley @ Palo Alto > > ______________________________________________ > 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. >-- Joshua Wiley Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/
Gabor Grothendieck
2010-Aug-22 22:31 UTC
[R] how to implement string pattern extraction in R
On Sun, Aug 22, 2010 at 6:05 PM, Waverley @ Palo Alto <waverley.paloalto at gmail.com> wrote:> Hi, > > In perl, to get a substring matching a particular ?pattern can be > implemented like the following example: > > $x = "AAAA.txt"; > if ($x=~ /(.*?)\.txt/){ > ?$prefix = $1; > } > > So how to do the same thing in R? > > Can someone provide me the code sample? >Try any of these: x <- "AAAA.txt" # 1 sub("(.*)\\.txt", "\\1", x) # 2 sub(".txt$", "", x) # 3 strsplit(x, "\\.")[[1]][1] #4 library(gsubfn) strapply(x, "(.*)\\.txt", simplify = c)
Waverley @ Palo Alto
2010-Aug-22 22:51 UTC
[R] how to implement string pattern extraction in R
Thanks for the reply to pointing me to the grep functions. I have checked the readme page http://pbil.univ-lyon1.fr/library/base/html/grep.html before I sent the help request. Just don't know how to extract a substring matching a pattern out of a string. Can someone give me the example code similar to that in perl to extract the prefix out of the string. Thanks much. On Sun, Aug 22, 2010 at 3:05 PM, Waverley @ Palo Alto <waverley.paloalto at gmail.com> wrote:> Hi, > > In perl, to get a substring matching a particular ?pattern can be > implemented like the following example: > > $x = "AAAA.txt"; > if ($x=~ /(.*?)\.txt/){ > ?$prefix = $1; > } > > So how to do the same thing in R? > > Can someone provide me the code sample? > > Thanks much in advance. > > -- > Waverley @ Palo Alto >-- Waverley @ Palo Alto