0

I have a string of the form:

" { \"Name1\":\"Value1\",\"Name2\":\"Value2\",\"Name3\":\"Value3\" } "

except with about 50 such pairs. I need to Search for some 10 names out of these 50 (I don't know their position, I only know the name) and replace it's corresponding Value with a string "newValue".

How do I go about this? I though of finding the location of Name, let us say it was from char 30-40, then use a regex of type ( \" .* \" ) from char 40 onwards to find the complete size of Value. , Then I can replace. Sadly the some of the values themselves contain strings of type ( \" ...... \" ) and anything else (including commas, backslashes, quotes etc..)so I don't think this will work. Any suggestions?

Not a bug
  • 4,286
  • 2
  • 40
  • 80
Kartik_Koro
  • 1,277
  • 1
  • 11
  • 23
  • 3
    That looks like JSON, in which case a JSON library would be much better than regex. – Biffen Jul 04 '14 at 09:20
  • As [Biffen](http://stackoverflow.com/users/418066/biffen) implies, use a JSON parser instead of regex. Typically `org.json`, GSON or my favorite, Jackson. – Mena Jul 04 '14 at 09:23
  • @Biffen I know it looks like json, but it actually isn't (it is invalid json because of all the backslashes), it's just a string that is sent to the server and the server parses this string into json for its own use. (the server needs the backslashes before quotes for parsing)A little weird I know, but I can't do anything about how the server side works, I have to send a string with such escaped quotes. – Kartik_Koro Jul 04 '14 at 09:24
  • try to pass string to https://code.google.com/p/google-gson/ and see is it able to consume it. By the way u do have test string u do know extected result. write some test and make some class impelement different way to do job and see what satisfy u most. – simar Jul 04 '14 at 09:24
  • @Kartik_Koro looks like the `String` programmatic representation of a JSON. Hence you should be able to consume it. – Mena Jul 04 '14 at 09:25
  • Couldn't you unescape the quotes, and then use a json parser? – stripybadger Jul 04 '14 at 09:28
  • @Mena Yes I suppose I can easily pass it into org.json's JSONObject constructor, I should get a JSONObject out of it. I can then easily change the value of a name I want. But as I said I need to send this type of string WITH backslashes again to server, so I would have to use toString method of the JSONObject, but again it won't have the slashes. I can try replacing all " with \" but this will replace the " in values as well, which will lead to undesired results. – Kartik_Koro Jul 04 '14 at 09:28
  • @stripybadger Making it into a json object is easy, but I need to send a string of this type to server with escaped quotes. Getting back such a string from JSONObject will be a pain I think – Kartik_Koro Jul 04 '14 at 09:30
  • @Kartik_Koro post the original string in pastebin, and give me the uploaded link. – Avinash Raj Jul 04 '14 at 09:32
  • @Kartik_Koro are you implying that the server you send this to does not de-serialize it, but actually requires to read the String double-escaped? In that case I feel sorry for the mess you're in :( – Mena Jul 04 '14 at 09:32

3 Answers3

0

Is that JSON ? If so, I would much rather use the appropriate parser and its API rather than trying to come up with a regexp to handle a well-defined grammar.

Community
  • 1
  • 1
Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • I know it looks like json, but it actually isn't (it is invalid json because of all the backslashes), it's just a string that is sent to the server and the server parses this string into json for its own use. (the server needs the backslashes before quotes for parsing)A little weird I know, but I can't do anything about how the server side works, I have to send a string with such escaped quotes. – Kartik_Koro Jul 04 '14 at 09:25
0

Not a very efficient solution..

public static void main(String[] args) {
    String s = " { \"Name1\":\"Value1\",\"Name2\":\"Value2\",\"Name3\":\"Value3\" } ";
    String name = "Name2", value = "mynewValue";
    String myRegex = name + ".*,";
    String newValue = name + "\\\":\\" +"\""+ value + "\\\",";
    String p = s.replaceAll(myRegex, newValue);
    System.out.println(s);
    System.out.println(p);
}

O/P :

{ "Name1":"Value1","Name2":"Value2","Name3":"Value3" }

{ "Name1":"Value1","Name2":"mynewValue","Name3":"Value3" }

example -2 :

{ "Name1":"Value1","Name2":"Va,lue2","Name3":"Value3" }

{ "Name1":"Value1","Name2":"mynewValue","Name3":"Value3" }

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
  • I'm sorry if I forgot to mention, the Value can be absolutely anything, i.e. it can contain character ',' as well. Doesn't this fail when Value2 is Val,ue2 instead of Value2? – Kartik_Koro Jul 04 '14 at 09:39
  • @Kartik_Koro - Nopes.. `.*` is greedy modifier. It gobbles up the input and checks for the biggest match (i.e, takes the largest possible match) :).. Check my second example.. – TheLostMind Jul 04 '14 at 09:42
  • Hmm, Ill try this out in my actual project, could you explain a bit about how this works? I guess my understanding of regex is not that good, then how come .* doesn't gobble up the entire remaining amount of the string including name3:value3? – Kartik_Koro Jul 04 '14 at 09:47
  • I don't understand, It failed for me on the first comma, I got this result: Old: \"DESC_FULL\":\"Brief Bug Description On network failure, records which are got failed wont get displayed \" ..... NEW:\"DESC_FULL\":"mynewValue", records which are got failed wont get displayed \" – Kartik_Koro Jul 04 '14 at 09:58
0

I think you can still use a Json parsing. Parse and manipulate th value like a Json object and then modify your string to apply all the back slashes. For example with Gson you can do like

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;


public class TenWords {
    public static void main(String[] args) throws Exception {
        String json = " { \"Name1\":\"Value1\",\"Name2\":\"Value2\",\"Name3\":\"Value3\" } ";

        Map<String, String> obj = new HashMap<String, String>();
        Gson gson = new Gson();
        obj = gson.fromJson(json, obj.getClass());
        obj.put("Name2", "NewValue");
        System.out.println(gson.toJson(obj).replaceAll("\"","\\\\\""));
    }
}

Output

{\"Name3\":\"Value3\",\"Name1\":\"Value1\",\"Name2\":\"NewValue\"}

The only problem is the order of name-value pair wont be maintained, but since your server is treating this as a Json it should be a problem.

Syam S
  • 8,421
  • 1
  • 26
  • 36