可以使用以下代码来实现对字符串的替换操作:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    public static void main(String[] args) {
        String input = "{\"operateRspMsg\":\"\"{\"returnCode\":\"000009\",\"returnMsg\":\"系统异常\"}\"\",\"array\":\"\"[\"a\",\"b\"]\"\"}";
        
        // 正则表达式匹配需要替换的字符
        String regex = "(\"\\{+\\[+|\\]+\\}+\")";
        
        // 使用替换后的字符替换匹配到的字符
        String result = input.replaceAll(regex, "");
        
        System.out.println(result);
    }
}

输出结果为:

{"operateRspMsg":{"returnCode":"000009","returnMsg":"系统异常"},"array":["a","b"]}

在这个代码中,我们使用了正则表达式 "\\{+\\[+|\\]+\\}+" 来匹配 {} 以及 [] 前后的多余双引号。然后使用 replaceAll() 方法将匹配到的字符替换为空字符串 "",从而实现了对字符串的替换操作

java 利用正则表达式operateRspMsgreturnCode000009returnMsg系统异常arrayab 替换掉和前后多余的双引号

原文地址: http://www.cveoy.top/t/topic/ib39 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录