class MediaWikiUtil {

    static String removeBetween(String string, char begin1, char begin2, char end1, char end2) {
        if (string.length() < 2) {
            return string;
        }
        // XXX some regex, iteratively applied, like "\\{\\{[^{}]*\\}\\}", might also work
        CharStack charStack = new CharStack();
        charStack.push(string.charAt(0));
        for (int idx = 1; idx < string.length(); idx++) {
            char previous = string.charAt(idx - 1);
            char current = string.charAt(idx);
            if (current == end2 && previous == end1) { // closing brackets
                while (charStack.length() > 1) {
                    // remove from stack until we found opening brackets
                    if (charStack.pop() == begin2 && charStack.peek() == begin1) {
                        charStack.pop();
                        // in case, closing brackets follow immediately, advance on the index by one
                        if (idx < string.length() - 1 && string.charAt(idx + 1) == end1) {
                            idx++;
                        }
                        break;
                    }
                }
                // use a space as placeholder for removed content;
                // this way, we do not run into confusion with tags e.g. '' '' instead of ''''
                charStack.push(' ');
            } else {
                charStack.push(current);
            }
        }
        return charStack.toString();
    }

    private  MediaWikiUtil();

}