fork download
  1. import java.util.*;
  2. import java.util.regex.*;
  3.  
  4. class Ideone
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. String[] find = {"girl", "boy"};
  9. String[] replace = {"boy", "girl"};
  10.  
  11. Map<String, String> dictionary = new HashMap<String, String>();
  12. for (int i = 0; i < find.length; i++) {
  13. dictionary.put(find[i], replace[i]);
  14. }
  15.  
  16. String str = "boy girl loop for get out boy girl left right";
  17. Matcher m = Pattern.compile("\\b(?:" + String.join("|", find) + ")\\b").matcher(str);
  18. System.out.println( m.replaceAll(r -> dictionary.get(r.group())) );
  19. }
  20. }
Success #stdin #stdout 0.11s 50836KB
stdin
Standard input is empty
stdout
girl boy loop for get out girl boy left right