[R] regex problems with the escape character

ravi rv15i at yahoo.se
Mon Aug 17 09:21:56 CEST 2009


Hi R-users and R-experts,
I am having a hard time in figuring out how to tackle regex questions where the "backslash" character is an integral part of the string. Let me explain how I came across this problem :
I wanted to clearly see all the components in the windows environmental path variable. This is a long string. For easy readability, I wanted to split up this string so that each component starts on a new line. But I ran smack into a problem in the very first step - in reading in the string. Look at the following code :

##### path variable has been shortened to show only the first few components
s1<- "C:\Rtools\bin;C:\Rtools\perl\bin;C:\Rtools\MinGW\bin;C:\Program\MiKTeX 2.7\miktex\bin;"
s2<-gsub(";",";\n",s1)
cat(s2,"\n") 

I get the following warning messages after the first line :
Warning messages:
1: '\R' is an unrecognized escape in a character string 
2: '\R' is an unrecognized escape in a character string 
3: '\p' is an unrecognized escape in a character string 
4: '\R' is an unrecognized escape in a character string 
5: '\M' is an unrecognized escape in a character string 
6: '\P' is an unrecognized escape in a character string 
7: '\M' is an unrecognized escape in a character string 
8: '\m' is an unrecognized escape in a character string 
9: unrecognized escapes removed from "C:\Rtools\bin;C:\Rtools\perl\bin;C:\Rtools\MinGW\bin;C:\Program\MiKTeX 2.7\miktex\bin;" 

I thought about attempting to escape the escape character and so on. Is that a workable option here? However, for this to work, I must first be able to read in the string correctly. The simplest solution, if it is at all possible, would be to temporarily change the escape character. To "%", for example. Is such a declaration possible? Other alternatives?
I would appreciate help in understanding different ways of solving this problem.
Thanking you,
Ravi





More information about the R-help mailing list