Match All Against Separation
Matching and splitting are two sides of the same coin. This is quite complicated because Java does not support recursion, and we have several nested parentheses. But this should do the trick:
Java
\(.*?\)(?![^(]*\))|[^\s(]+
See the demo .
To iterate over all matches:
Pattern regex = Pattern.compile("\\(.*?\\)(?![^(]*\\))|[^\\s(]+");
Matcher regexMatcher = regex.matcher(subjectString);
while (regexMatcher.find()) {
}
Explanation
\(.*?\)(?![^(]*\)) , . (simple(nesting)) , (this(kind)of(nesting)) (. PHP)| ...[^\s(]+ , par
PHP-
PHP ( Java (this(kind)of(nesting)):
(\((?:[^()]++|(?1))*\))|[^\s(]+