[R] How to store regex expression in a variable

Peter Dalgaard pdalgd at gmail.com
Sat Sep 25 09:42:24 CEST 2010


On 09/25/2010 03:45 AM, Yong Wang wrote:
> dear list
> 
> I know how to store a regex expression in perl and ruby, no clue on R.
> I do read R regex manual , archives, and searched on line,
> still I need somebody help me out on how to store a regular expression
> in a variable.

A regex is just a character string in R. So, e.g.

>      str <- 'Now is the time      '
>      sub(' +$', '', str)  ## spaces only
[1] "Now is the time"
> re <- ' +$'
>      sub(re, '', str)  ## spaces only
[1] "Now is the time"

-- 
Peter Dalgaard
Center for Statistics, Copenhagen Business School
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list