1

My String is like this.

{\\\"692950841314120\\\":[{\\\"type\\\":\\\"ads_management\\\",\\\"call_count\\\":3,\\\"total_cputime\\\":1,\\\"total_time\\\":5,\\\"estimated_time_to_regain_access\\\":0}]}

Since the key here is a variable value I am trying to replace this 692950841314120(or the values which I get from sever) with a constant like ID. My main goal is to parse this as POJO. I have tried using..

string.replaceAll("^[0-9]{15}$","ID")

but due to Slashes I think i am not able to get the desired value. Is there any better way to do this. I know I can do below Code but I don't want any ID123 if I added extra value and distort any other info in JSON.

string.replaceAll("[0-9]{15}","ID")
4
  • 1
    Maybe replace ^(\{\\{3}\")\d{15,} with $1ID. That way you would only remove they key at the start of the JSON and you could add more numbers to this key at the server's side. Commented Aug 26, 2021 at 9:15
  • Do you mean you want to extract that value? Commented Aug 26, 2021 at 9:35
  • no replace 692950841314120 with something else like ID. Its a key and is also keeps changing. I have to give @serializedName to this key in my POJO class. So that i can parse it. This value will always be 15 digit. Commented Aug 26, 2021 at 9:40
  • 1
    Then it is safer to assume the value is inisde \" and \":. Use .replaceAll("(\\\\\")[0-9]{15}(\\\\\":)", "$1ID$2") Commented Aug 26, 2021 at 9:42

3 Answers 3

2

Strictly speaking, if you have a valid JSON string, you should parse it using something like GSON, rather than using regex. That being said, if you must use regex, you could try removing the starting and ending anchors:

string.replaceAll("[0-9]{15}", "ID")

Or maybe use double quotes instead:

string.replaceAll("\"[0-9]{15}\"", "ID")
Sign up to request clarification or add additional context in comments.

Comments

1

It is safer to assume the value is inisde \" and \":.

You can then use

.replaceAll("(\\\\\")[0-9]{15}(\\\\\":)", "$1ID$2")

The regex is (\\")[0-9]{15}(\\":) and it means:

  • (\\") - match and capture \" substring into Group 1
  • [0-9]{15} - fifteen digits
  • (\\":) - Group 2: a \": substring.

The $1 and $2 are placeholders holding the Group 1 and 2 values.

Comments

1

You should use "A word boundary" \b.

Try this.

public static void main(String[] args) {
    String input = "{\\\"692950841314120\\\":"
        + "[{\\\"type\\\":\\\"12345678901234567890\\\","
        + "\\\"call_count\\\":3,"
        + "\\\"total_cputime\\\":1,"
        + "\\\"total_time\\\":5,"
        + "\\\"estimated_time_to_regain_access\\\":0}]}";
    System.out.println(input.replaceAll("\\b[0-9]{15}\\b", "ID"));
}

output:

{\"ID\":[{\"type\":\"12345678901234567890\",\"call_count\":3,\"total_cputime\":1,\"total_time\":5,\"estimated_time_to_regain_access\":0}]}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.