I need to validate a user given String and validate that it is a valid Set, possibly a set that contains inner sets. Examples:
1) {1, 2, 3, 4} = valid
2) {1, 2, {3, 4}, 5} = valid
3) 1, 2, 3, 4 = invalid (missing brackets)
4) {1, 2, {3, 4, 5} = invalid (missing inner bracket)
This is the regex I am using (broken up for readability):
String elementSeparator = "(,\\s)?";
String validElement = "(\\{?[A-Za-z0-9]*\\}?" + elementSeparator + ")*";
String regex = "^\\{" + validElement + "\\}$";
Currently the it accepts Sets with optional opening and closing brackets, but I need it to only accept if they are both there, and not if an inner set is missing a bracket. In my current implementation the 4th example is accepted as a valid set.
How can I accomplish this?