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

Java 如何撤消这种基于正则表达式的压缩?,java,regex,regex-group,Java,Regex,Regex Group,我压缩了我的二进制字符串,它输出3131,持续6 1s(111111),这代表了我在这段代码中找到的@tigrang的字符“p” 现在我需要解压这个字符串“3131”,并使其输出111111。我如何在不使用循环的情况下执行此操作? 有没有办法将其压缩得更大,例如:输出61而不是3131 public static String compress_string(String inp) { String compressed = ""; Pattern pattern = Patte

我压缩了我的二进制字符串,它输出3131,持续6 1s(111111),这代表了我在这段代码中找到的@tigrang的字符“p”

现在我需要解压这个字符串“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 + "'");
}
公共静态字符串压缩\u字符串(字符串inp){
字符串压缩=”;
Pattern=Pattern.compile(“([\\w])\\1*”;
匹配器匹配器=模式匹配器(inp);
while(matcher.find()){
String group=matcher.group();
如果(group.length()>1)压缩+=group.length()+“”;
压缩+=组字符(0);
}
返回压缩;
}
公共静态字符串解压\u字符串(字符串inp){
StringBuilder s=新的StringBuilder();
对于(int i=0;i
3131可以表示313 x“1”吗?不,3131只能表示111111。但它应该输出3140,用于p这项工作,但问题是我已经在for循环中,我想最小化时间复杂度,但我会尝试一下,然后返回给你。
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 + "'");
}