fork 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.  
  12. public static String map(final String input) {
  13. if ("you".equals(input)) {
  14. return "SO";
  15. } else if ("me".equals(input)) {
  16. return "Kate";
  17. }
  18. return "?"+input+"?";
  19. }
  20. public static void main (String[] args) throws java.lang.Exception
  21. {
  22. String input = "hello [you], this is [me]";
  23. Pattern p = Pattern.compile("\\[([^\\]]*)\\]");
  24. Matcher m = p.matcher(input);
  25. StringBuffer bufStr = new StringBuffer();
  26. boolean flag = false;
  27. while ((flag = m.find())) {
  28. String toReplace = m.group(1);
  29. m.appendReplacement(bufStr, map(toReplace));
  30. }
  31. m.appendTail(bufStr);
  32. String result = bufStr.toString();
  33. System.out.println(result);
  34. }
  35. }
Success #stdin #stdout 0.09s 320320KB
stdin
Standard input is empty
stdout
hello SO, this is Kate