如何撤销这种基于正则表达式的压缩?

How to undo this regular expression based compression?

我压缩了我的二进制字符串,它输出 3131 为 6 1s(111111),代表字符 'p' 来自我在这个 link.

中找到的 @tigrang 代码
     public static String compress_string(String inp) {
        String compressed = "";
        Pattern pattern = Pattern.compile("([\w])\1*");
        Matcher matcher = pattern.matcher(inp);
        while(matcher.find()) {
           String group = matcher.group();
           if (group.length() > 1) compressed += group.length() + "";
           compressed += group.charAt(0);
        }
        return compressed;
    }

现在我需要解压缩这个字符串“3131”并使其输出111111。我如何在不使用循环的情况下做到这一点? 有没有办法进一步压缩它,例如:输出 61 而不是 3131?

怎么样:

public static String compress_string(String inp) {
    String compressed = "";
    Pattern pattern = Pattern.compile("([\w])\1*");
    Matcher matcher = pattern.matcher(inp);
    while (matcher.find()) {
        String group = matcher.group();
        if (group.length() > 1) compressed += group.length() + "";
        compressed += group.charAt(0);
    }
    return compressed;
}

public static String decompress_string(String inp) {
    StringBuilder s = new StringBuilder();
    for (int i = 0; i < inp.length(); i++) {
        char ch = inp.charAt(i);
        if (ch == '1') {
            s.append('1');
        } else {
            int count = ch - '0';
            String repeat = "" + inp.charAt(++i);
            s.append(String.join("", Collections.nCopies(count, repeat)));
        }
    }
    return s.toString();
}

public void test(String[] args) throws Exception {
    String test = "111111";
    String compressed = compress_string(test);
    String decompressed = decompress_string(compressed);
    System.out.println("test = '" + test + "' compressed = '" + compressed + "' decompressed = '" + decompressed + "'");
}