fork(4) download
  1. import java.util.regex.*;
  2.  
  3. class Ideone {
  4. public static void main (String[] args) {
  5. String[] names = { "dave", "richard", "danny" };
  6. System.out.println(parse("hello $2", names));
  7. }
  8.  
  9. public static String parse(String command, String[] args) {
  10. Matcher m = Pattern.compile("\\$(\\d+)").matcher(command);
  11. while (m.find()) {
  12. int num = Integer.parseInt(m.group(1));
  13. m.appendReplacement(sb, args[num - 1]);
  14. }
  15. m.appendTail(sb);
  16. return sb.toString();
  17. }
  18. }
Success #stdin #stdout 0.04s 4386816KB
stdin
Standard input is empty
stdout
hello richard