java - How to write regex to extract pattern from string -


i have json string , want extract information need care about. want extract value of key 'name' json string, there may multiple entries of 'name'.

e.g json string :

{"key1":"value1","key2":"value2","key3":"value3","name":"allen","detail":{"detailkey1":"detailvalue2","name":"john","detailkey2":"detailvalue2"}} 

i need extract 'allen' , 'john' json string.

i not familiar regex , hope can on it.

if can give sample code in java, that's great.

thanks

updated: everybody's reply , tried myself , it's pretty simple. copy code below

final string mydata = "{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\",\"name\":\"allen\",\"detail\":{\"detailkey1\":\"detailvalue2\",\"name\":\"john\",\"detailkey2\":\"detailvalue2\"}}";      final pattern pattern = pattern.compile("\"name\":\"(.*?)\"");     final matcher matcher = pattern.matcher(mydata);     while (matcher.find()) {         system.out.println(matcher.group(1));     } 

i suggest parse using json parser. here's how java org.json parser.

jsonobject jsonobject = new jsonobject(jsonstring); system.out.println(jsonobject.getstring("name")); // allen  jsonobject detail = jsonobject.getjsonobject("detail"); system.out.println(detail.getstring("name")); // john 

to support multiple names, you'd have embedded json array, instead of repeating key.

jsonobject jsonobj = new jsonobject("{\"name\": [\"mike ross\", \"rachel zane\"]}"); system.out.println(jsonobj.getjsonarray("name").getstring(1)); // rachel zane