String reformatExpression(String exp) {

        int i = 0;
        char it;
        int itCount;
        boolean haveStar;
        StringBuffer buff = new StringBuffer();
        int size = exp.length();

        while (i < size) {
            it = exp.charAt(i);
            itCount = 1;
            haveStar = false;
            char next;
            if (i < size - 1)
                next = exp.charAt(i + 1);
            else
                next = '\0';
            while ((next == it || next == '*') && (i < size - 1)) {
                if (next == it)
                    itCount++;
                else {
                    haveStar = true;
                    itCount--;
                }
                i++;
                if (i < size - 1)
                    next = exp.charAt(i + 1);
                else
                    next = '\0';
            }
            for (int j = 0; j < itCount; j++)
                buff.append(it);
            if (haveStar) {
                buff.append(it);
                buff.append('*');
            }
            ;
            i++;
        }

        return buff.toString();
    }