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

Phil Spector spector at stat.berkeley.edu
Fri Apr 9 22:47:26 CEST 2010


David -
     Here's the last paragraph of the "Details" section
of the regex help page:

  Patterns are described here as they would be printed by ‘cat’:
  (_do remember that backslashes need to be doubled when entering R
  character strings_, e.g. from the keyboard).

You can get around this restriction using readline:

> pat = readline()
\t
> pat
[1] "\\t"
> cat(pat,'\n')
\t

It also should be remembered that R will add an extra backslash
when it prints a single backslash -- as can be seen, this is 
avoided when you use cat().


 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu



On Fri, 9 Apr 2010, David.Epstein wrote:

>
> 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'),
> 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