fork(1) download
  1. import java.util.ArrayList;
  2. import java.util.HashMap;
  3. import java.util.List;
  4.  
  5. class _16319749 {
  6. public static void main(String[] args) {
  7. nonregex();
  8. regex();
  9. }
  10.  
  11. public static void nonregex() {
  12. String poem = "In software, a stack overflow [apple] occurs"
  13. + " when too much memory [orange] is used on the call stack [banana]."
  14. + " The call stack [pear] contains a limited amount of memory,"
  15. + " often determined at the start of the program [apple].";
  16.  
  17. HashMap<String, String> rep = new HashMap<String, String>();
  18.  
  19. rep.put("[apple]", "<img src='apple.jpg' />");
  20. rep.put("[banana]", "<img src='banana.jpg' />");
  21. rep.put("[orange]", "<img src='orange.jpg' />");
  22. rep.put("[pear]", "<img src='pear.jpg' />");
  23.  
  24. for (String fruit : rep.keySet())
  25. poem = poem.replace(fruit, rep.get(fruit));
  26.  
  27. System.out.println(poem);
  28. }
  29.  
  30. public static void regex() {
  31. String poem = "In software, a stack overflow [apple] occurs"
  32. + " when too much memory [orange] is used on the call stack [banana]."
  33. + " The call stack [pear] contains a limited amount of memory,"
  34. + " often determined at the start of the program [apple].";
  35.  
  36. List<String> fruits = new ArrayList<String>();
  37. fruits.add("[apple]");
  38. fruits.add("[banana]");
  39. fruits.add("[pear]");
  40. fruits.add("[orange]");
  41.  
  42. String pattern = "\\[(?<=\\[)(\\w+)(?=])\\]";
  43. poem = poem.replaceAll(pattern, "<img src='$1.jpg' />");
  44.  
  45. System.out.println(poem);
  46. }
  47. }
  48.  
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
In software, a stack overflow <img src='apple.jpg' /> occurs when too much memory <img src='orange.jpg' /> is used on the call stack <img src='banana.jpg' />. The call stack <img src='pear.jpg' /> contains a limited amount of memory, often determined at the start of the program <img src='apple.jpg' />.
In software, a stack overflow <img src='apple.jpg' /> occurs when too much memory <img src='orange.jpg' /> is used on the call stack <img src='banana.jpg' />. The call stack <img src='pear.jpg' /> contains a limited amount of memory, often determined at the start of the program <img src='apple.jpg' />.