[R] perhaps regular expression bug with | sign ??

William Dunlap wdunlap at tibco.com
Fri Apr 9 22:52:32 CEST 2010


> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of David.Epstein
> Sent: Friday, April 09, 2010 1:36 PM
> To: r-help at r-project.org
> Subject: [R] perhaps regular expression bug with | sign ??
> 
> 
> Here is my interaction with R:
> > sub(x='>|t|',pattern = '|t',replacement='zz')
> [1] "zz>|t|"
> 
> So I say to myself "Clearly the | signs need to be escaped, 
> so let's try
> this"
> > sub(x='>|t|',pattern = '\|t',replacement='zz')
> [1] "zz>|t|"
> Warning messages:
> 1: '\|' is an unrecognized escape in a character string 
> 2: unrecognized escape removed from "\|t"
> How can \| be an unrecognized escape? This flatly contradicts 
> help('regex'),

It would be a bit clearer if the warnings indicated that
they were from the R parser (the function that converts
your text input to R expressions which are later evaluated).
The parser is trying to say that it is treating "\|" as "|".
The backlash has special meaning in things like "\n" (newline),
"\t" (tab), and "\123" (character number in octal), but not
before a vertical bar.

Because the parser removed the backslash sub() never saw it. 

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> or am I misunderstanding the help?
> 
> The first pattern above works if one uses extended=F.
> 
> What do R experts think?
> 
> David
> -- 
> View this message in context: 
> http://n4.nabble.com/perhaps-regular-expression-bug-with-sign-
> tp1819872p1819872.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 



More information about the R-help mailing list