I want to split the string by \"/\" and ignore \"/\" inside the outer parentheses.
Sample input string:
\"Apple 001/(Orange (002/003) ABC)/Mango 003
Here's a sample of a parser that would implement your need :
public static List<String> splitter(String input) {
int nestingLevel=0;
StringBuilder currentToken=new StringBuilder();
List<String> result = new ArrayList<>();
for (char c: input.toCharArray()) {
if (nestingLevel==0 && c == '/') { // the character is a separator !
result.add(currentToken.toString());
currentToken=new StringBuilder();
} else {
if (c == '(') { nestingLevel++; }
else if (c == ')' && nestingLevel > 0) { nestingLevel--; }
currentToken.append(c);
}
}
result.add(currentToken.toString());
return result;
}
You can try it here.
Note that it doesn't lead to the expected output you posted, but I'm not sure what algorithm you were following to obtain such result. In particular I've made sure there's no "negative nesting level", so for starters the /
in "Mango 003 )/( ASDJ"
is considered outside of parenthesis and is parsed as a separator.
Anyway I'm sure you can tweak my answer much more easily than you would a regex answer, the whole point of my answer being to show that writing a parser to handle such problems is often more realistic than to bother trying to craft a regex.