[R] : regular expressions: escaping a dot

Tobias Verbeke tobias.verbeke at businessdecision.com
Thu Jun 28 09:56:47 CEST 2007


Wolfram Fischer wrote:

> What's really the problem with:
> 
>> regexpr( '\.odt$', "xxxxYodt", perl=TRUE )
> 	Warning: '\.' is an unrecognized escape in a character string
> 	Warning: unrecognized escape removed from "\.odt$"
> 	[1] 5
> 	attr(,"match.length")
> 	[1] 4
> 
> I know that I could use:
>> regexpr( '[.]odt$', "xxxxYodt", perl=TRUE )
> 
> But it seems to me that the first expression is also
> an accepted regular expression in accordance with perl.

In R you have to escape the "\".

 From the help page of regexpr:

## Double all 'a' or 'b's;  "\" must be escaped, i.e., 'doubled'
gsub("([ab])", "\\1_\\1_", "abc and ABC")

HTH,
Tobias

-- 

Tobias Verbeke - Consultant
Business & Decision Benelux
Rue de la révolution 8
1000 Brussels - BELGIUM

+32 499 36 33 15
tobias.verbeke at businessdecision.com



More information about the R-help mailing list