I'm trying to split a string on ||
, &&
, and ()
, and I'm not able to properly split on nested parenthesis.
Example string:
q1 == false || ( q1 == true && q3 != null && ( method(param) - method() ) > 120 )
My current regex / code:
String[] tempTokens = input.split("(?=([|]{2}|[&]{2}|[(]|[)]))|(?<=([|]{2}|[&]{2}|[(]|[)]))");
for (String token : tempTokens) {
if (token.trim().length() > 0) {
System.out.println(token.trim());
}
}
Current output:
q1 == false
||
(
q1 == true
&&
q3 != null
&&
(
method
(
param
)
- method
(
)
)
> 120
)
Wanted output:
q1 == false
||
(
q1 == true
&&
q3 != null
&&
( method(param) - method() ) > 120
)
Basically, I'm trying to tokenize an expression, and I want to split on the parenthesis only if they contain a full statement that contains a >
, >=
, ==
, etc.