1

I am getting a String like below:

...,{foo.....}","gm:every":["one:varyingalphanumberic193alpha"],"{bar...},....

Now, checking if Pattern exists in the string, I want to get rid of this pattern - "gm:every":["one:varyingalphanumberic193alpha"], so that my string becomes:

...,{foo.....}","{bar...},....

I tried to follow this - How to remove a particular pattern from a String in Java? but in my case the given pattern can be anywhere in the string and also contains varying alpha-numeric characters. Feeling clueless on how to compile it. Any pointer in this regard, will be greatly appreciated.

Ajay Kumar
  • 2,906
  • 3
  • 23
  • 46

2 Answers2

1

Yes its a member of JSON

import org.json.*;

JSONObject jsonObject = new JSONObject(jsonString);
jsonObject.remove("gm:every");

jsonObject.toString();
Rukshan Jayasekara
  • 1,945
  • 1
  • 6
  • 26
  • 1
    This is perfectly fine @Rukshan. But as we discussed initially, I have to deal with the String only as it is passed to multiple teams. So every team is handling that String differently. Thanks a lot anyways. – Ajay Kumar Oct 20 '21 at 17:05
1
String text = "...,{foo.....}\",\"gm:every\":[\"one:varyingalphanumberic193alpha\"],\"{bar...},....";

text = text.replaceAll("\"gm:every\":\\[\"one:[\\s\\S]*],", "");

System.out.println(text);
philoopher97
  • 772
  • 1
  • 6
  • 18