code: private void appendEvaluated(StringBuffer buffer, String s) {
          boolean escape = false;
          boolean dollar = false;
  
          for (int i = 0; i < s.length(); i++) {
              char c = s.charAt(i);
              if (c == '\\' && !escape) {
                  escape = true;
              } else if (c == '$' && !escape) {
                  dollar = true;
              } else if (c >= '0' && c <= '9' && dollar) {
                  buffer.append(group(c - '0'));
                  dollar = false;
              } else {
                  buffer.append(c);
                  dollar = false;
                  escape = false;
              }
          }
  
          if (escape) {
              throw new ArrayIndexOutOfBoundsException(s.length());
          }
      }
docs: Internal helper method to append a given string to a given string buffer.
  If the string contains any references to groups, these are replaced by
  the corresponding group's contents.
  
  @param buffer the string buffer.
  @param s the string to append.