I've never used a regex before and I am looking to split up a file that has one or more JSON objects, the JSON objects are not separated by a comma. So I need to split them between "}{" and keep both curly braces. This is what the string looks like:
{id:"123",name:"myName"}{id:"456",name:"anotherName"}
I would like a string array like using string.split()
["{id:"123",name:"myName"}", "{"id:"456",name:"anotherName"}"]
If your objects aren't more complex than what you show, you may use lookarounds like this :
String[] strs = str.split("(?<=\\})(?=\\{)");
Exemple :
String str = "{id:\"123\",name:\"myName\"}{id:\"456\",name:\"yetanotherName\"}{id:\"456\",name:\"anotherName\"}";
String[] strs = str.split("(?<=\\})(?=\\{)");
for (String s : strs) {
System.out.println(s);
}
prints
{id:"123",name:"myName"}
{id:"456",name:"anotherName"}
{id:"456",name:"yetanotherName"}
If your objects are more complex, a regex wouldn't probably work and you would have to parse your string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.