javarestintellij-ideaweb-api-testing

How to replace repetitive pattern in a string in Java?


I have intelliJ running with Java/REST API and having trouble removing and replacing string in a certain format.

Response string :

{"isSuccess":[{"frequency":}]}]}}{"isSuccess":[{"frequency":}]}]}}{"isSuccess":<testData>[{"frequency":<testData>}]}]}}

Expected string :

[{"frequency":}]},{"frequency":<testData>}]},{"frequency":<testData>}}]}]}}

Actual String :

[{"frequency":<testData>}]}]}}[{"frequency":<testData>}]}]}}[{"frequency":"<testData>}]}]}}

Code :

public static String getResponseString(String response){
        String getIDErrorCodeString = response.split("\\[")[0];                 
        String getStringWithoutIDErrorCode = response.replaceAll(Pattern.quote(getIDErrorCodeString), "");
        String getStringwithBracket = "}]}]" + getStringWithoutIDErrorCode + "}}[{";

        String actualResults = getStringWithoutIDErrorCode.replaceAll(Pattern.quote(getStringwithBracket), ",");

        return actualResults;
}

The original string had the }}[{ in the beginning of the line which got removed in the actual results but still pattern is not recognizing and finding the correct string to replace not just in the beginning but in the entire string. Anyone knows how to do this?


Solution

  • It was very simple than I wrote originally,

    public static String getResponseString(String response){
            String getIDErrorCodeString = response.split("\\[")[0];                  
            
            String getStringWithoutIDErrorCode = response.replaceAll(Pattern.quote(getIDErrorCodeString), "");
            String actualResults = getStringWithoutIDErrorCode.replaceAll("]}}\\[", ",");
    
        return actualResults;