public static String replaceVariable(String line, String varName, String replacement) {
        String result = "";
        String rest = line;
        boolean done = false;
        while (!done) {
            Matcher m = VAR_PATTERN.matcher(rest);
            if (m.matches()) {
                String var = m.group(2);
                // System.out.println("Match on line: " + rest + " for var: " + var);
                if (var.equals(varName) && replacement != null) {
                    result = replacement + rest.substring(m.end(1), rest.length()) + result;
                    rest = rest.substring(0, m.start(1));
                } else {
                    result = rest.substring(m.start(1), rest.length()) + result;
                    rest = rest.substring(0, m.start(1));
                }
            } else {
                result = rest + result;
                done = true;
            }
        }
        return result;
    }