Replace part of the String by Regular Expression

Top.Deck

My String input is something like:

{"key1":"value1","key2":"{\"key2_1\":\"123\",\"key2_2\":\"456\",\"key2_3\":\"33333\"}"}

The value fields in the above JSON could contain characters such as ", \ and so on. For your convience here is the formatted version:

{
    "key1": "value1",
    "key2": "{\"key2_1\":\"123\",\"key2_2\":\"456\",\"key2_3\":\"33333\"}"
}

I want to use Gson to convert the String into a Foo Object:

class Foo {
    private String key1;
    private Bar key2;
    ...
}

class Bar {
    private String key2_1;
    private String key2_2;
    private String key2_3;
    ...
}

Here's my regular expression:

String regexp = "\\{[\"a-zA-Z-0-9:,\\\\]*\"key2\":\"\\{\\\\\"key2_1\\\\\":\\\\\"[a-zA-Z0-9]*\\\\\",\\\\\"key2_2\\\\\":\\\\\"[a-zA-Z0-9]*\\\\\",\\\\\"key2_3\\\\\":\\\\\"[a-zA-Z0-9]*\\\\\"\\}\"\\}[\"a-zA-Z-0-9:,\\\\]*";
Pattern pattern = Pattern.compile(regexp);
Matcher matcher = pattern.matcher(text);
if(matcher.matches()) {
    ... // TODO: Replace all "{, \" and }" but How???
}

How could I use this regular expression to replace all "{, \".and "} into {, ", } without changing the keys and values in JSON?

Finding the sub-string and using String's replace method will be my backup solution.

Again, my ultimate goal is to parse the input String into my Foo object. Is there a better way rather than using regular expression?

Thank you!

Top.Deck

After digging further, I find those two links:

Gson custom deseralizer for one variable in an object

Gson custom seralizer for one variable (of many) in an object using TypeAdapter

I achieved what I want by registering my own JsonDeserializer to GsonBuilder:

private static GsonBuilder gsonBuilder = new GsonBuilder().registerTypeAdapter(Bar.class, new JsonDeserializer<Bar>() {

    @Override
    public Bar deserialize(JsonElement json, Type typeOfT,
            JsonDeserializationContext context) throws JsonParseException {
        Bar result = new Bar();
        String regexp = "\"\\{\\\\\"key2_1\\\\\":\\\\\"(?s).*\\\\\".\\\\\"key2_2\\\\\":\\\\\"(?s).*\\\\\",\\\\\"key2_3\\\\\":\\\\\"(?s).*\\\\\"\\}\"";
        Pattern pattern = Pattern.compile(regexp);
        Matcher matcher = pattern.matcher(json.toString());
        String modifiedJsonStr = json.toString();
        if(matcher.matches()) {
            modifiedJsonStr = json.toString().replace("\"{", "{").replace("}\"", "}").replace("\\\"", "\"");
        }
        result = new Gson().fromJson(modifiedJsonStr, Bar.class);
        return result;
    }
});

Cheers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Regular Expression to replace part of a string

From Dev

Regular expression to replace a string

From Dev

Regular expression for getting part of string

From Dev

Regular expression to retain part of the string

From Dev

Replace query string with regular expression?

From Dev

Multiple string replace with a Regular Expression

From Dev

php regular expression string replace

From Dev

Java String replace Regular Expression

From Dev

Regular Expression: replace string in javascript

From Dev

Replace word in string, regular expression

From Dev

Replace string using Regular expression

From Dev

Notepad++ regular expression - replace a part of regex

From Dev

C# Regular Expression and Replace on part of the match

From Dev

Regular Expression to match part or all of a string

From Dev

Exclude part of string using Regular expression

From Dev

Extracting a specific part of a string with regular expression in Python

From Dev

Regular expression - Get specific part of string

From Dev

mask part of a string using regular expression

From Dev

How to capture a String expression and replace only a part of it?

From Dev

How to capture a String expression and replace only a part of it?

From Java

Python string.replace regular expression

From Dev

Regular Expression to Replace All But One Character In String

From Dev

How to replace a string in a regular expression match

From Dev

Find regular expression string and replace it in R

From Dev

Regular Expression - detect Specific Url and replace that string

From Dev

Regular expression Replace to remove numbers in prefix to the string

From Dev

replace String with another in java using regular Expression

From Dev

Replace string with regular expression and my own parameter

From Dev

replace a string in all occurence using regular expression

Related Related

  1. 1

    Regular Expression to replace part of a string

  2. 2

    Regular expression to replace a string

  3. 3

    Regular expression for getting part of string

  4. 4

    Regular expression to retain part of the string

  5. 5

    Replace query string with regular expression?

  6. 6

    Multiple string replace with a Regular Expression

  7. 7

    php regular expression string replace

  8. 8

    Java String replace Regular Expression

  9. 9

    Regular Expression: replace string in javascript

  10. 10

    Replace word in string, regular expression

  11. 11

    Replace string using Regular expression

  12. 12

    Notepad++ regular expression - replace a part of regex

  13. 13

    C# Regular Expression and Replace on part of the match

  14. 14

    Regular Expression to match part or all of a string

  15. 15

    Exclude part of string using Regular expression

  16. 16

    Extracting a specific part of a string with regular expression in Python

  17. 17

    Regular expression - Get specific part of string

  18. 18

    mask part of a string using regular expression

  19. 19

    How to capture a String expression and replace only a part of it?

  20. 20

    How to capture a String expression and replace only a part of it?

  21. 21

    Python string.replace regular expression

  22. 22

    Regular Expression to Replace All But One Character In String

  23. 23

    How to replace a string in a regular expression match

  24. 24

    Find regular expression string and replace it in R

  25. 25

    Regular Expression - detect Specific Url and replace that string

  26. 26

    Regular expression Replace to remove numbers in prefix to the string

  27. 27

    replace String with another in java using regular Expression

  28. 28

    Replace string with regular expression and my own parameter

  29. 29

    replace a string in all occurence using regular expression

HotTag

Archive