I need to check the user who is given the String and verify that he is a valid Set, possibly a set containing internal 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 that I use (broken for readability):
String elementSeparator = "(,\\s)?"; String validElement = "(\\{?[A-Za-z0-9]*\\}?" + elementSeparator + ")*"; String regex = "^\\{" + validElement + "\\}$";
Currently, it accepts sets with additional opening and closing brackets, but I need to accept it only if they are both, and not if there is no bracket in the inner set. In my current implementation, the 4th example is accepted as a valid set.
How can i do this?
java regex
unmuse
source share