title: In Java Regular Expression "\1" (back reference) is not working
body: <p>I want to replace string <code>"Cannot"</code> with <code>"Can not"</code> and <code>"cannot"</code> with <code>"can not"</code>. For that, I am using the code below:</p>
  
  <pre><code> String string = "I Cannot do it.";
   string = string.replaceAll("([Cc])annot", "\\1an not");
  </code></pre>
  
  <p>Desired string is <code>"I Can not do it."</code>.</p>
  
  <pre><code>String string = "I Cannot do it.";
  string = string.replaceAll("([Cc])annot", "\\1an not");
  </code></pre>
  
  <p>Desired string is <code>"I can not do it"</code>. In Ruby <code>'\1'</code> replaces a string with the matched character <code>C</code> or <code>c</code> (using back reference). I don't know what to use in Java. Below is the Ruby regex which works fine:</p>
  
  <pre><code>"I Cannot do it".gsub!(/([Cc])annot/,'\1an not')
  # =&gt; "I Can not do it"       
  "I cannot do it".gsub!(/([Cc])annot/,'\1an not')
  # =&gt; "I can not do it"
  </code></pre>