title: Why can 'hallo\nworld' match both \n and \\n in R?
body: <p>Why does <code>grep</code> treat <code>\n</code>  and  <code>\\n</code> the same way ?  </p>
  
  <p>For example, both match <code>hallo\nworld</code>.</p>
  
  <pre><code>grep("hallo\nworld", pattern="\n")
  [1] 1
  grep("hallo\nworld", pattern="\\n")
  [1] 1
  </code></pre>
  
  <p>I see that <code>hallo\nworld</code> is parsed into   </p>
  
  <pre><code>hallo  
  world
  </code></pre>
  
  <p>that is, <code>hallo</code> on one line and <code>world</code> on one line.</p>
  
  <p>So in <code>grep("hallo\nworld", pattern="\n")</code>, is the <code>pattern="\n"</code> a new line or <code>\n</code> literally?</p>
  
  <p>Also note this happens with others; <code>\a</code> <code>\f</code> <code>\n</code> <code>\t</code> <code>\r</code> and <code>\\a</code> <code>\\f</code> <code>\\n</code> <code>\\t</code> <code>\\r</code> are all treated identically.  But <code>\d</code> <code>\w</code> <code>\s</code> can't be used!  Why not?</p>
  
  <p>I chose different strings to test, and I found the secret in the concept of regular expression.    </p>
  
  <p>There are two concepts of escape, one is escape in a string, it is simple to understand; the other is escape in a regular pattern expression string.  In R a pattern such as <code>grep(x, pattern=" some string here ")</code>,  <code>\\n</code>=<code>\n</code>= a newline character. But in common string, <code>\\n</code> !=<code>\n</code> ,the former is literally <code>\n</code>,the latter is a newline character. We can prove this by :</p>
  
  <pre><code>cat("\n")
  
  cat("\\n")
  \n&gt; 
  </code></pre>
  
  <p>How to prove this? I'll try with other characters, not just <code>\n</code>, to see if they match in the same way.</p>
  
  <pre><code>special1 &lt;- c( "\a", "\f", "\n", "\t", "\r")
  special2 &lt;- c("\\a","\\f","\\n","\\t","\\r")
  target &lt;- paste("hallo", special1, "world", sep="")
  for (i in 1:5){
      cat("i=", i, "\n")
      if( grep(target[i], pattern=special1[i]) == 1)
          print(paste(target[i], "match", special1[i], "succeed"))
      if( grep(target[i], pattern=special2[i]) == 1)
          print(paste(target[i], "match", special2[i], "succeed"))
  }
  </code></pre>
  
  <p>output:</p>
  
  <pre><code>i= 1   
  [1] "hallo\aworld match \a succeed"  
  [1] "hallo\aworld match `\\a` succeed"  
  i= 2   
  [1] "hallo\fworld match \f succeed"  
  [1] "hallo\fworld match `\\f` succeed"  
  i= 3   
  [1] "hallo\nworld match \n succeed"  
  [1] "hallo\nworld match `\\n` succeed"  
  i= 4   
  [1] "hallo\tworld match \t succeed"  
  [1] "hallo\tworld match `\\t` succeed"  
  i= 5   
  [1] "hallo\rworld match \r succeed"  
  [1] "hallo\rworld match `\\r` succeed" 
  </code></pre>
  
  <p>Note that <code>\a</code> <code>\f</code> <code>\n</code> <code>\t</code> <code>\r</code> and  <code>\\a</code> <code>\\f</code> <code>\\n</code> <code>\\t</code> <code>\\r</code>  were all treated identically in R regular pattern expression string!</p>
  
  <p>Not only that, you can not write <code>\d</code> <code>\w</code> <code>\s</code> in an R regular expression pattern!<br>
  You can write any of these:</p>
  
  <pre><code>pattern="\a" "pattern=\f" "pattern=\n" "pattern=\t" "pattern=\r"
  </code></pre>
  
  <p>But you can't write any of these!</p>
  
  <pre><code>pattern="\d" "pattern="\w" "pattern=\s"  in grep.
  </code></pre>
  
  <p>I think this is also a bug , as <code>\d</code> <code>\w</code> <code>\s</code> are treated unequally to <code>\a</code> <code>\f</code> <code>\n</code> <code>\t</code> <code>\r</code>.</p>