Search code examples
javaregexpattern-matchingbean-validationcharacter-properties

@Pattern with Unicode script \\p{L}* doesn't work


I have problem with javax.validation.constraints.Pattern @Pattern validation.

@Pattern(regexp = "\\p{L}*", message = "Msg")
private String name;

When I'm trying to input any text it doesn't work.

When I used:

@Pattern(regexp = "[a-zA-Z]*", message = "Msg")

It works great with non latin characters.


Solution

  • You need to make the \p{L} pattern Unicode aware with the Pattern.UNICODE_CHARACTER_CLASS flag.

    Enables the Unicode version of Predefined character classes and POSIX character classes.

    Since you are using the string pattern, you may use the inline (embedded) flag variant, (?U):

    regexp = "(?U)\\p{L}*"