[R] how to implement string pattern extraction in R

Joshua Wiley jwiley.psych at gmail.com
Mon Aug 23 00:18:32 CEST 2010


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/



More information about the R-help mailing list