code: public static String preg_replace(Pattern pattern, String replacement, String subject) {
          Matcher m = pattern.matcher(subject);
          StringBuffer sb = new StringBuffer(subject.length());
          while(m.find()){ 
              m.appendReplacement(sb, replacement);
          }
          m.appendTail(sb);
          return sb.toString();
      }
docs: Matches a string with a pattern and replaces the matched components with
  a provided string.
  
  @param pattern
  @param replacement
  @param subject
  @return