Search code examples
javaregexio

Truncate text from server with Java


I'm receiving a message from my server like so:

/713.105.135.149:54969: [{"row 0":[{},{},{},{},{},{},{},{}]},{"row 1":[{},{},{},{},{},{},{},{}]},{"row 2":[{},{},{},{},{},{},{},{}]},{"row 3":[{},{},{},{},{},{},{},{}]},{"row 4":[{},{},{},{},{},{},{},{}]},{"row 5":[{"column 0":"WhitePawn"},{},{},{},{},{},{},{}]},{"row 6":[{},{},{},{},{},{},{},{}]},{"row 7":[{},{},{},{},{},{},{},{}]}]

I'd like to encode it as JSON, so I need to truncate all the stuff that isn't JSON, i.e. I need to axe this text /713.105.135.149:54969:.

Maybe I can use regex to string replace everything before [{"?

Is it possible to do that? if so, how?


Solution

  • You don't need a regular expression. You can just use

    String s = "/713.105.135.149:54969: [{\"row 0\": ...";
    String trimmed = s.substring(s.indexOf("["));
    System.out.println(trimmed);  // [{"row 0": ...