I want to accept only those titles which don't contain following characters: \/:*?"<>|
I tried using the below method, but it didn't work for me
@Pattern(regexp = "[^\\/:*?\"<>|]", message = "Not valid")
private String title;
I am new in pattern matching so please help me with the solution...
A good start. The @Pattern
annotation defines the pattern the underlying String should match.
The annotated CharSequence must match the specified regular expression. The regular expression follows the Java regular expression conventions see Pattern.
In your case, you only list the characters, that should not be matched. You should define a pattern to be matched instead.
Try this one (see Regex101 for a demo) and notice the +
quantifier.
^[^\/:*?\"<>|]+$
In Java, mind the escaping:
@Pattern(regexp = "^[^\\/:*?\\\"<>|]+$", message = "Not valid")
private String title;