I have a java string that looks like this;
(fname:jon)(lname:doe)(guaranteer: Sam (W) Willis)(age:35)(addr:1 Turnpike Plaza)(favcolor:blue)
And I want to split this String from delimiter (fname:jon)<here>(lname:doe)
.
I tried splitting through regex \)\(
but it just breaks my code
arr = s.split("\\)\\(");
for (String a: arr) System.out.println(a);
Output
(fname:jon
lname:doe
guaranteer: Sam (W) Willis
age:35
addr:1 Turnpike Plaza
favcolor:blue)
I also looked at this question: How to split a string, but also keep the delimiters?, but it didn't helped because in my case, I want to keep the delimiter )(
and split delimiter evenly as well, i.e., the first bracket should go to first result and second to second result.
The regex that i used was s.split("(?<=\\)\\()")
and it gave output:
(fname:jon)(
lname:doe)(
guaranteer: Sam (W) Willis)(
age:35)(
addr:1 Turnpike Plaza)(
favcolor:blue)
This is my desired output:
(fname:jon)
(lname:doe)
(guaranteer: Sam (W) Willis)
(age:35)
(addr:1 Turnpike Plaza)
(favcolor:blue)