[R] regex question

ravi rv15i at yahoo.se
Tue Aug 4 19:28:10 CEST 2009


Hi,
I am getting stuck over an apparently simple problem in the use of regular expressions :
To collect together the first letters of the words from the Perl motto, “There is more than one way to do it” in the following form – TIMTOWTDI. 
I tried the following code :
 
##### A regex problem with the Perl motto
astr<-"There is more than one way to do it"
b1<-grep("\\<", astr,value=T)
## This just retrieves  the whole string
## Next trial with gregexpr
b2<-gregexpr("\\<",astr)
## This gives  :
> b3
[[1]]
[1]  1  7 10 15 20 24 28 31 34
attr(,"match.length")
[1] 0 0 0 0 0 0 0 0 0
 
A vector of indices corresponding to the first letter is obtained all right with gregexpr but the next step is not so clear. I am not able to figure out how I can use this information to pick out the letters from the original string. My problem is that I don’t know how I can treat the string as a vector and pluck out the letters.
 
There may be many ways to do it, but I have not succeeded in coming up with even one way! I will appreciate any tips that I can get.
Thanking you,
Ravi




More information about the R-help mailing list