I have a following string that I would like to parse into either a List or a String[].
(Test)(Testing (Value))
End result should be Test
and Testing (Value)
I have a following string that I would like to parse into either a List or a String[].
(Test)(Testing (Value))
End result should be Test
and Testing (Value)
If I understood correctly that you need Test
and Testing (Value)
, then here is regexp:
\(([^\)]+)\)\((.+)\)
and masked version of it ready for java string:
\\(([^\\)]+)\\)\\((.+)\\)
Please read this section of php manual. It tells you about recursive patterns, which can be used to match nested things, and about the related problems. Google it for more information.
This question is really vague, but under one reasonable interpretation, this is the solution that can take care of arbitrary nesting depth in a certain format:
String text = "(T)(T(V))(1(2(3)2)1)(a*(b+(c-d))+(e/f))";
String[] parts = text.split("(?<=\\))(?=\\()");
System.out.println(java.util.Arrays.toString(parts));
// prints "[(T), (T(V)), (1(2(3)2)1), (a*(b+(c-d))+(e/f))]"
Basically you want to split
between )(
(using assertions). Won't work for all cases, e.g. ((.)(.))
, but like many people have said, the requirements of the question is vague, and the general balanced parenthesis problem is not solvable with Java regex.