import javax.validation.constraints.Pattern; public class A { @Pattern(regexp = "") private String values; // getter and setter } public class Main { public static void main(String[] args) { ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); A a = new A(); a.setValues("name1:value1,name2:value,name10:value7"); Set<ConstraintViolation<A>> constraintViolations = validator.validate(a); System.out.println(constraintViolations.size()); System.out.println(constraintViolations.iterator().next().getMessage()); } } Field
private String values;contains comma separated values (name: value, ..., ...). Where "name" and "value" should not be repeated. Separator ","
How to check in java language validity through one regex using @Pattern?:
- If Comma separated values is valid - it contains the correct structure "name1: value1, name2: value, name10: value7"
- If the "name" does not repeat