Reputation: 388
I have an annotation that can have value of a parameter as the string representation of the array of string (e.g. "[\"Value1\", \"Value2\"]"). So basically the value of the annotation is a string. E.g.
@MyAnn(value = "[\"V1\"]")
Valid set of the strings can only be either:
value = "[\"V1\", \"V2\", \"\"]"
value = "[ ]"
value = "[]"
value = ""
A legal regex that matches these strings is:
value = (?:\"\[\s*(\\\".*\")*\s*\]\"|\"\"|\"\[\s*\]\")
I'd like to have a checkstyle such that if when someone is using @MyAnn
then the value must be one of the 4. Anything else should be reported as the violation.
The problem I am having is how to specify the regex for the invalid values (mine is for valid values). Because the checkstyle needs me to specify the regex for the illegal values.
What would be the way to work it out?
Upvotes: 0
Views: 74
Reputation: 1934
Surround your regex with the ?!
negative lookahead assertion:
^((?!(?:\"\[\s*(\\\".*\")*\s*\]\"|\"\"|\"\[\s*\]\")).)*$
See Checkstyle - How to exclude any file, but the ones ending with 'Impl.java', with a regex?
Upvotes: 0