title: How to escape $ in java?
body: <p>I am trying below code but getting error</p>
  
  <pre><code>String x = "aaa XXX bbb";
      String replace = "XXX";
      String y = "xy$z";
      String z=y.replaceAll("$", "\\$");
      x = x.replaceFirst(replace, z);
      System.out.println(x);
  </code></pre>
  
  <p>Error</p>
  
  <pre><code>Exception in thread "main" java.lang.IllegalArgumentException: Illegal group reference
      at java.util.regex.Matcher.appendReplacement(Unknown Source)
      at java.util.regex.Matcher.replaceFirst(Unknown Source)
      at java.lang.String.replaceFirst(Unknown Source)
      at Test.main(Test.java:10)
  </code></pre>
  
  <p>I want result as</p>
  
  <pre><code>aaa xy$z bbb
  </code></pre>