title: Why String.replaceAll() in java requires 4 slashes "\\\\" in regex to actually replace "\"?
body: <p>I recently noticed that, String.replaceAll(regex,replacement) behaves very weirdly when it comes to the escape-character "\"(slash)</p>
  
  <p>For example consider there is a string with filepath - <code>String text = "E:\\dummypath"</code>
  and we want to replace the <code>"\\"</code> with <code>"/"</code>.</p>
  
  <p><code>text.replace("\\","/")</code> gives the output <code>"E:/dummypath"</code> whereas <code>text.replaceAll("\\","/")</code> raises the exception <code>java.util.regex.PatternSyntaxException</code>.</p>
  
  <p>If we want to implement the same functionality with <code>replaceAll()</code> we need to write it as, 
  <code>text.replaceAll("\\\\","/")</code></p>
  
  <p>One notable difference is <code>replaceAll()</code> has its arguments as reg-ex whereas <code>replace()</code> has arguments character-sequence!</p>
  
  <p>But <code>text.replaceAll("\n","/")</code> works exactly the same as its char-sequence equivalent <code>text.replace("\n","/")</code></p>
  
  <p><strong>Digging Deeper:</strong>
  Even more weird behaviors can be observed when we try some other inputs.</p>
  
  <p>Lets assign <code>text="Hello\nWorld\n"</code></p>
  
  <p>Now,
  <code>text.replaceAll("\n","/")</code>, <code>text.replaceAll("\\n","/")</code>, <code>text.replaceAll("\\\n","/")</code> all these three gives the same output <code>Hello/World/</code></p>
  
  <p>Java had really messed up with the reg-ex in its best possible way I feel! No other language seems to have these playful behaviors in reg-ex. Any specific reason, why Java messed up like this?</p>