import java.util.*;
import java.util.regex.*;

class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
		String[] find = {"girl", "boy"};
		String[] replace = {"boy", "girl"};
		 
		Map<String, String> dictionary = new HashMap<String, String>();
		for (int i = 0; i < find.length; i++) {
		    dictionary.put(find[i], replace[i]);
		}
		 
		String str = "boy girl loop for get out boy girl left right";
		Matcher m = Pattern.compile("\\b(?:" + String.join("|", find) + ")\\b").matcher(str);
		System.out.println( m.replaceAll(r -> dictionary.get(r.group())) );
	}
}