[R] Cannot use an escape character in regexp

Duncan Murdoch murdoch at stats.uwo.ca
Fri May 11 13:20:23 CEST 2007


On 11/05/2007 7:07 AM, Vittorio wrote:
> Given the string
> 
>> mystr <- "(Preconsuntivo  del  giorno gas 10 maggio 
> 2007)Tj"
> 
> I'm trying to detect and eliminate the string ")Tj" at the 
> very end of mystr by means of 
> gsub(rx2,"",mystr) BUT preparing the 
> matching regexp string a warning pops up
> 
>> rx2 <- "\)Tj$"
> Warning 
> messages:
> 1: '\)' is an unrecognized escape in a character string 
> 2: 
> unrecognized escape removed from "\)Tj$" 
> 
> and
> 
>> rx2
> [1] ")Tj$"
> 
> I 
> tried also rx2 <- "(\))Tj$" but the result is the same.
> 
> What am I 
> missing?

You want the escaping to occur in gsub, not in R.  So you want to escape 
the escape:

rx3 <- "\\)Tj$"
gsub(rx3, "", mystr)

Duncan Murdoch



More information about the R-help mailing list