[R] characters in a string

Marc Schwartz marc_schwartz at me.com
Wed Sep 15 17:32:45 CEST 2010


On Sep 15, 2010, at 10:16 AM, rajeshj at cse.iitm.ac.in wrote:

> 
> Hi,
> 
> I need to check if a string "<rh>a,b,c,d<rh>" is delimited by two "<rh>" 's as efficiently as possible(I need to do this a lot of times) and return TRUE. Can someone suggest a good technique? 


See ?grep and ?regex

> grepl("^<rh>.*<rh>$", "<rh>a,b,c,d<rh>")
[1] TRUE


You can pass the entire source vector to grepl():

Vec <- c("<rh>a,b,c,d<rh>", "1, 2, 3, 4", "a, b, c, d<rh>", "<rh>1, 2, 3, 4<rh>")

> grepl("^<rh>.*<rh>$", Vec)
[1]  TRUE FALSE FALSE  TRUE


HTH,

Marc Schwartz



More information about the R-help mailing list