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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -