-1

Trying to replace multi line string in java using replaceAll method but it's not working. Is there anything wrong with below logic?

    String content="      \"get\" : {\n" + 
    "        \"name\" : [ \"Test\" ],\n" + 
    "        \"description\" : \"Test description to replace\",\n" + 
    "        \"details\" : \"Test details\"";


    String searchString="        \"name\" : [ \"Test\" ],\n" + 
"        \"description\" : \"Test description to replace\",";


String replaceString="        \"name\" : [ \"Actual\" ],\n" + 
"        \"description\" : \"Replaced description\",";

Tried below options and none of them worked-

Pattern.compile(searchString, Pattern.MULTILINE).matcher(content).replaceAll(replaceString);

Pattern.compile(searchString, Pattern.DOTALL).matcher(content).replaceAll(replaceString);

content = content.replaceAll(searchString, replaceString);
springenthusiast
  • 403
  • 1
  • 8
  • 30
  • I have no way to test this at the moment, but you definitely need to escape the `[` brackets in your search string (change them to `\\[`). Maybe that will already fix it. – Tim Pietzcker Apr 02 '19 at 06:46
  • 1
    you can quote the whole search string with `\Q` at the beginning or call `Pattern.quote()` – Sharon Ben Asher Apr 02 '19 at 06:47
  • Use Something like this : String str = content.replaceAll("\\n",""); – Coder Apr 02 '19 at 07:15
  • Since you are not using regular expressions, use either the simple `content = content.replace(searchString, replaceString);` or the more complex `content = Pattern .compile(searchString, Pattern.LITERAL).matcher(content).replaceAll(replaceString);`. The latter makes sense if you want to keep the result of `compile` and use it multiple times. Then, you get a benefit from the preparation, e.g. the Boyer–Moore algorithm used behind the scenes. – Holger Apr 02 '19 at 10:06

1 Answers1

0

DISCLAIMER: You should not use regex to manipulate JSON or XML that have infinite nested content. Finite automate are not adapted to manipulate those data structures and you should use a JSON/XML parser instead.

This being said, purely for learning purpose, I will quickly fix your code.

1) Use either replace instead of replaceAll to avoid that your searchString is interpreted as a regex:

String content="      \"get\" : {\n" + 
            "        \"name\" : [ \"Test\" ],\n" + 
            "        \"description\" : \"Test description to replace\",\n" + 
            "        \"details\" : \"Test details\"";


String searchString="        \"name\" : [ \"Test\" ],\n" + 
        "        \"description\" : \"Test description to replace\",";


String replaceString="        \"name\" : [ \"Actual\" ],\n" + 
        "        \"description\" : \"Replaced description\",";

System.out.println(content.replace(searchString, replaceString));

output:

  "get" : {
    "name" : [ "Actual" ],
    "description" : "Replaced description",
    "details" : "Test details"

2) Or use replaceAll but escape the brackets to avoid them being interpreted as a character class definition attempt.

String searchString="        \"name\" : \\[ \"Test\" \\],\n" + 
        "        \"description\" : \"Test description to replace\",";


String replaceString="        \"name\" : [ \"Actual\" ],\n" + 
        "        \"description\" : \"Replaced description\",";

System.out.println(content.replaceAll(searchString, replaceString));

output:

  "get" : {
    "name" : [ "Actual" ],
    "description" : "Replaced description",
    "details" : "Test details"

Link: How to parse JSON in Java

  • You should load your json structure in an object
  • Change the values of the attributes of that object to the new values
  • Export it again in json format
Allan
  • 12,117
  • 3
  • 27
  • 51