I have strings with this format: “a,b, c,d” and this format: “a(b,c,d)”
I want to split on ‘,’ or ‘, ‘ but I want to terminate splitting when I encounter the ‘(‘ in the second format.
This is what I had before I started hacking.
String [] stringArray = string.split(", |,");
The array of the first format would contain: ‘a’, ‘b’, ‘c’, ‘d’
The array of the second format would conaint ‘a(b,c,d)’
Example:
String string1 = "ab,cd, de";
String string2 = "ab(de,ef);
String [] array1 = string1.split(...);
String [] array2 = string2.split(...);
array1 result: ["ab" "cd" "de"]
array2 result: ["ab(de,ef)"]
The number of characters between the commas are not limited. I hope this is more clear.
Thanks.
If you know the parentheses are always properly balanced and they’ll never be nested inside other parens, this will work:
If the lookahead finds a
)without seeing a(first, it must be inside a pair of parens. Given this string:…
resultwill be: