java - Getting string in side curly braces that also has more values in curly braces within it -
so here's (java) string
string s = "some string preceding {\"key1\": \"val1\", \"key2\": {\"embedkey1\": \"embedval1\", \"embedkey2\": \"embedval2\"}, \"key3\" : \"val3\", \"key3\": \"val4\"}, value proceeding it" i want within outer curly braces. how do that? far i've tried following
pattern p = pattern.compile("\\{([^}]*)\\}"); matcher m = p.matcher(s); while(m.find()){ system.out.println(m.group(1)); } this however, prints
"key1": "val1", "key2": {"embedkey1": "embedval1", "embedkey2": "embedval2" can please me this?
to in between outer braces, or between first { , last }, use greedy matching . matching symbols (with dotall mode):
string s = "some string preceding {\"key1\": \"val1\", \"key2\": {\"embedkey1\": \"embedval1\", \"embedkey2\": \"embedval2\"}, \"key3\" : \"val3\", \"key3\": \"val4\"}, value proceeding it"; pattern p = pattern.compile("(?s)\\{(.*)}"); matcher m = p.matcher(s); while(m.find()){ system.out.println(m.group(1)); } see ideone demo
the (?s) inline version of pattern.dotall modifier.
Comments
Post a Comment