[R] R Regular Expressions - Metacharacters
    Duncan Murdoch 
    murdoch.duncan at gmail.com
       
    Tue Feb  5 22:26:46 CET 2013
    
    
  
On 05/02/2013 12:49 PM, Seth Dickey wrote:
> I thought that I can use metacharacters such as \w to match word characters
> with one backslash.  But for some reason, I need to include two backslashes.
>
> > grepl(pattern='\w', x="what")
> Error: '\w' is an unrecognized escape in character string starting "\w"
>
> > grepl(pattern='\\w', x="what")
> [1] TRUE
>
> I can't find the reason for this on the help pages.  Does anyone know why?
grepl wants a string containing a single backslash.  R uses the 
backslash as an escape character, so you need to double it in your 
source, so the string ends up containing just one.
"\w" is interpreted by R as an escaped w, which doesn't make sense.
"\\w" is interpreted by R as a backslash followed by a w, and then the 
\w is interpreted by grepl the way you want.
Duncan Murdoch
    
    
More information about the R-help
mailing list