fork(3) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.util.regex.*;
  5. import java.lang.*;
  6. import java.io.*;
  7.  
  8. /* Name of the class has to be "Main" only if the class is public. */
  9. class Ideone
  10. {
  11. public static void main (String[] args) throws java.lang.Exception
  12. {
  13. String tree = "(30:0.07,(1:0.06,2:0.76))";
  14. HashMap<String, String> h = new HashMap<String, String>();
  15. h.put("(1:" , "(30:");
  16. h.put(",1:" , ",30:");
  17. h.put("(30:" , "(6:");
  18. h.put(",30:" , ",6:");
  19. System.out.println(convertTree(tree, h));
  20.  
  21. }
  22. private static String convertTree(String treeOld, HashMap<String, String> conv) {
  23. Pattern pattern = Pattern.compile("[,(]\\d+:");
  24. Matcher m = pattern.matcher(treeOld);
  25. StringBuffer result = new StringBuffer();
  26. while (m.find()) {
  27. if (conv.containsKey(m.group(0))) {
  28. m.appendReplacement(result, conv.get(m.group(0)));
  29. }
  30. else {
  31. m.appendReplacement(result, m.group(0));
  32. }
  33. }
  34. m.appendTail(result);
  35. return result.toString();
  36.  
  37. }
  38. }
Success #stdin #stdout 0.11s 320576KB
stdin
Standard input is empty
stdout
(6:0.07,(30:0.06,2:0.76))