fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.regex.*;
  5. /* Name of the class has to be "Main" only if the class is public. */
  6. class Ideone
  7. {
  8. public static void main (String[] args) throws java.lang.Exception
  9. {
  10. SortedMap<String, String> map = new TreeMap<String, String>();
  11. map.put("test", "REPLACE");
  12. String update = "$(test) (test) (test2)";
  13. Matcher m = Pattern.compile("\\$\\(([^)]*)\\)").matcher(update);
  14.  
  15. while (m.find()) {
  16. String t = map.getOrDefault(m.group(1), m.group(1));
  17. m.appendReplacement(sb, t);
  18. }
  19. m.appendTail(sb); // append the rest of the contents
  20. System.out.println(sb);
  21. }
  22.  
  23. }
Success #stdin #stdout 0.1s 320512KB
stdin
Standard input is empty
stdout
REPLACE (test)      (test2)