fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4. import java.util.function.Function;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. class Ideone {
  9. public static void main (String[] args) throws java.lang.Exception {
  10. Pattern pattern = Pattern.compile("([a-z]+)");
  11. System.out.println(magic(
  12. pattern,
  13. "eee xxx 233 yyy zzz xxx",
  14. x -> "xxx".equals(x) ? "1" : "0"));
  15. }
  16. public static String magic(
  17. Pattern pattern,
  18. String input,
  19. Function<String, String> fun) {
  20. Matcher matcher = pattern.matcher(input);
  21. while (matcher.find()) {
  22. matcher.appendReplacement(sb, fun.apply(matcher.group()));
  23. }
  24. return sb.toString();
  25. }
  26. }
Success #stdin #stdout 0.2s 320768KB
stdin
Standard input is empty
stdout
0 1 233 0 0 1